package com.smartleanx.module.hrm.service.examine.examinSon;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.module.hrm.api.employee.EmployeeImplApi;
import com.smartleanx.module.hrm.controller.admin.examine.examinSon.vo.ExaminePageReqVO;
import com.smartleanx.module.hrm.dal.dataobject.dingDing.DingAttendanceConfirmation;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.ExamineFlowPathIndex.ExamineFlowPathIndexDo;
import com.smartleanx.module.hrm.dal.dataobject.examine.examinSon.ExamineDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineAssessmentScope.ExamineAssessmentScopeDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineDimension.ExamineDimensionDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineDimensionIndex.ExamineDimensionIndexDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineFlowPath.ExamineFlowPathDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineFlowPerformance.ExamineFlowPerformanceDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecord.ExamineRecordDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecordRater.ExamineRecordRaterDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineRecordRaterIndex.ExamineRecordRaterIndexDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResult.ExamineResultDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineResultConfig.ExamineResultConfigDO;
import com.smartleanx.module.hrm.dal.dataobject.examine.examineReview.ExamineReviewDO;
import com.smartleanx.module.hrm.dal.dataobject.salary.HrmSalaryRecordEmployee;
import com.smartleanx.module.hrm.dal.mysql.dingDing.DingAttendanceConfirmationMapper;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEContractTaskMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.ExamineFlowPathIndex.ExamineFlowPathIndexMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examinSon.ExamineMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineAssessmentScope.ExamineAssessmentScopeMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineDimension.ExamineDimensionMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineDimensionIndex.ExamineDimensionIndexMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineFlowPath.ExamineFlowPathMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineFlowPerformance.ExamineFlowPerformanceMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineProcess.ExamineProcessMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRangeType.ExamineRangeTypeMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRecord.ExamineRecordMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRecordRater.ExamineRecordRaterMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineRecordRaterIndex.ExamineRecordRaterIndexMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineResult.ExamineResultMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineResultConfig.ExamineResultConfigMapper;
import com.smartleanx.module.hrm.dal.mysql.examine.examineReview.ExamineReviewMapper;
import com.smartleanx.module.hrm.dal.mysql.salary.HrmSalaryRecordEmployeeMapper;
import com.smartleanx.module.hrm.eums.employee.EmployeeStatusEnum;
import com.smartleanx.module.hrm.eums.employee.EmploymentFormEnum;
import com.smartleanx.module.hrm.intergration.AdminUserServiceProvider;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTaskParam;
import com.smartleanx.module.hrm.service.examine.util.DateUtil;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import com.smartleanx.module.hrm.service.risk.RiskService;
import com.smartleanx.module.infra.api.file.FileApi;
import com.smartleanx.module.infra.api.file.dto.FileRefRespDTO;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.PostApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostRespDTO;
import com.smartleanx.module.system.api.notify.NotifyMessageSendApi;
import com.smartleanx.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import com.smartleanx.module.system.api.user.AdminUserApi;
import com.smartleanx.module.system.enums.notify.NotifyDetailTypeEnum;
import com.smartleanx.module.system.enums.notify.NotifyTemplateCodeEnum;
import lombok.val;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.health.RefreshScopeHealthIndicator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 考核计划 Service 实现类
 *
 * @author 宁无霜
 */
@Service
@Validated
public class ExamineServiceImpl implements ExamineService {

    @Resource
    private ExamineFlowPerformanceMapper examineFlowPerformanceMapper;
    @Resource
    private ExamineMapper examineMapper;
    @Resource
    private ExamineAssessmentScopeMapper examineAssessmentScopeMapper;
    @Resource
    private ExamineDimensionMapper examineDimensionMapper;
    @Resource
    private ExamineDimensionIndexMapper examineDimensionIndexMapper;
    @Resource
    private ExamineFlowPathMapper examineFlowPathMapper;
    @Resource
    private ExamineResultMapper examineResultMapper;
    @Resource
    private ExamineResultConfigMapper examineResultConfigMapper;
    @Resource
    private AdminUserServiceProvider adminUserServiceProvider;
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private ExamineRecordRaterIndexMapper examineRecordRaterIndexMapper;
    @Resource
    private ExamineRecordRaterMapper examineRecordRaterMapper;
    @Resource
    private ExamineReviewMapper examineReviewMapper;
    @Resource
    private ExamineProcessMapper examineProcessMapper;
    @Resource
    private ExamineRecordMapper examineRecordMapper;

    @Autowired
    private DingAttendanceConfirmationMapper dingAttendanceConfirmationMapper;

    @Autowired
    private HrmSalaryRecordEmployeeMapper hrmSalaryRecordEmployeeMapper;
    @Resource
    private ExamineRangeTypeMapper examineRangeTypeMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private DeptApi deptApi;

    @Resource
    private PostApi postApi;
    @Resource
    private RiskService riskService;
    @Resource
    private FileApi fileApi;
    @Resource
    private RefreshScopeHealthIndicator refreshScopeHealthIndicator;
    @Resource
    private NotifyMessageSendApi messageSendApi;

    @Resource
    private ExamineFlowPathIndexMapper examineFlowPathIndexMapper;
    @Autowired
    private HrmEContractTaskMapper hrmEContractTaskMapper;

    @Override
    public Integer createExamine(JSONObject param) {
//        final JSONObject data = param.getJSONObject("data");
        return dataProcessing(param);
    }

    @DataPermission(enable = false)
    private Integer dataProcessing(JSONObject param) {
        Integer id = param.getInteger("id");
        /**
         * 考核计划基础信息
         */
        //考核计划名称
        String examinePlanName = param.getString("examinePlanName");
        //考核周期
        String cycleTimeType = param.getString("cycleTimeType");
        String templateName = param.getString("templateName");
        Integer templateId = param.getInteger("templateId");
        //考核周期类型 1月度 2季度 3上半年 4下半年 5全年 6 其他
        Integer cycleType = param.getInteger("cycleType");
        String year = param.getString("year");
        Integer resultConfigTemplateId = param.getInteger("resultConfigTemplateId");
//        switch (cycleType) {
//            case 1:
//
//                break;
//            case 2:
//
//                break;
//            case 3:
//
//                break;
//            case 4:
//
//                break;
//            case 5:
//
//                break;
//            case 6:
//
//                break;
//
//        }
        //考核周期开始时间
        String startTime = param.getString("startTime");
        //考核周期结束时间
        String endTime = param.getString("endTime");
        //考核说明
        String illustrate = param.getString("illustrate");
        //评分范围
        Integer examineLimit = param.getInteger("examineLimit");
        Boolean value1 = param.getBoolean("value1");
        ExamineDO examineDO = new ExamineDO();
        examineDO.setExamineLimit(examineLimit);
        examineDO.setExaminePlanName(examinePlanName);
        examineDO.setExamineCycleEnd(endTime);
        examineDO.setExamineCycleStart(startTime);
        examineDO.setExamineState(-1);
        examineDO.setExamineIllustrate(illustrate);
        examineDO.setExamineCycleTypeId(cycleType);
        examineDO.setExamineYear(year);
        examineDO.setExamineCycleTimeType(cycleTimeType);
        examineDO.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
        examineDO.setTemplateId(templateId);
        examineDO.setTemplateName(templateName);
        examineDO.setResultConfigTemplateId(resultConfigTemplateId);
        if (value1) {
            examineDO.setIsReview(1);
        } else {
            examineDO.setIsReview(0);
        }
        examineMapper.insert(examineDO);
        int examineId = examineDO.getId();
        //考核范围
        List<JSONObject> assessmentScopeList = param.getList("assessmentScope", JSONObject.class);
        ArrayList<JSONObject> assessmentScopeArrayList = JSONObject.parseObject(JSONObject.toJSONString(assessmentScopeList), new ArrayList<JSONObject>().getClass());
        assessmentScopeArrayList.forEach(assessmentScope -> {
            //岗位列表
            List<Long> stationList = assessmentScope.getList("stationList", Long.class);
            //部门或岗位id
            Integer assessmentScopeId = assessmentScope.getInteger("assessmentScopeId");
            //考核范围 1部门/人员 2岗位
            Integer assessmentScopeType = assessmentScope.getInteger("assessmentScopeType");
            //人员Id列表
            List<Integer> employeeIds = assessmentScope.getList("employeeIds", Integer.class);
//            JSONObject data = assessmentScope.getJSONObject("data");
//            JSONArray jsonArray = data.getJSONArray("children");
//            List<Integer> employeeIds = new ArrayList<>();


            if (!ObjectUtils.isEmpty(stationList)) {
                employeeIds = new ArrayList<>();
                final List<Integer> userListByPostId = adminUserServiceProvider.getUserListByPostId(stationList);
                if (ObjectUtils.isEmpty(userListByPostId)) {
                    throw exception(POST_USER_ID_NOT_EXISTS);
                } else {
                    employeeIds.addAll(userListByPostId);
                }
            }

            if (ObjectUtils.isEmpty(assessmentScopeType)) {
                throw exception(ASSESSMENT_SCOPE_TYPE_NOT_EXISTS);
            }
            if (ObjectUtils.isEmpty(employeeIds)) {
                throw exception(ASSESSMENT_SCOPE_EMPLOYEE_ID_NOT_EXISTS);
            }
            employeeIds.forEach(employeeId -> {
                EmployeeDO employeeDO = employeeMapper.selectByUserId(employeeId.longValue());
                ExamineAssessmentScopeDO examineAssessmentScopeDO = new ExamineAssessmentScopeDO();
                examineAssessmentScopeDO.setExamineId(examineId);
                examineAssessmentScopeDO.setEmployeeId(employeeDO.getUserId().intValue());
                if (assessmentScopeType == 1) {
                    examineAssessmentScopeDO.setAssessmentScopeId(employeeDO.getDeptId().intValue());
                } else {
                    examineAssessmentScopeDO.setAssessmentScopeId(employeeDO.getPostId().intValue());
                    examineAssessmentScopeDO.setAssessmentPostId(employeeDO.getPostId().intValue());
                }
                examineAssessmentScopeDO.setAssessmentScopeType(assessmentScopeType);
                examineAssessmentScopeMapper.insert(examineAssessmentScopeDO);
            });
        });
        //考核维度多个
        List<JSONObject> examineDimensions = param.getList("examineDimensions", JSONObject.class);
        if (ObjectUtils.isEmpty(examineDimensions)) {
            throw exception(EXAMINE_DIMENSION_NOT_EXISTS);
        }
        ArrayList<JSONObject> examineDimensionList = JSONObject.parseObject(JSONObject.toJSONString(examineDimensions), new ArrayList<JSONObject>().getClass());
        examineDimensionList.forEach(examineDimension -> {
            ExamineDimensionDO examineDimensionDO = new ExamineDimensionDO();
            //维度名称
            String dimensionName = examineDimension.getString("dimensionName");
            //维度权重
            Integer dimensionWeight = examineDimension.getInteger("dimensionWeight");
            //维度备注
            String dimensionRemark = examineDimension.getString("dimensionRemark");
            String uuId = examineDimension.getString("uuId");
            examineDimensionDO.setExamineId(examineId);
            examineDimensionDO.setDimensionName(dimensionName);
            examineDimensionDO.setDimensionWeight(dimensionWeight);
            examineDimensionDO.setDimensionRemark(dimensionRemark);
            examineDimensionDO.setUuId(uuId);
            examineDimensionMapper.insert(examineDimensionDO);
            int examineDimensionId = examineDimensionDO.getId();
//            CollectionUtils.getSumValue(examineDimensionList,)
            //指标
            List<JSONObject> examineDimensionIndexs = examineDimension.getList("examineDimensionIndexs", JSONObject.class);
            if (ObjectUtils.isEmpty(examineDimensionIndexs)) {
                throw exception(EXAMINE_DIMENSION_INDEX_NOT_EXISTS);
            }
            ArrayList<JSONObject> examineDimensionIndexList = JSONObject.parseObject(JSONObject.toJSONString(examineDimensionIndexs), new ArrayList<JSONObject>().getClass());
            examineDimensionIndexList.forEach(examineDimensionIndex -> {
                //指标名称
                String indexName = examineDimensionIndex.getString("indexName");
                //指标说明
                String indexIllustrate = examineDimensionIndex.getString("indexIllustrate");
                //指标考核标准
                String indexStandard = examineDimensionIndex.getString("indexStandard");
                //指标权重
                Integer indexWeight = examineDimensionIndex.getInteger("indexWeight");
                //评分输入方式
                Integer indexType = examineDimensionIndex.getInteger("indexType");
                ExamineDimensionIndexDO examineDimensionIndexDO = new ExamineDimensionIndexDO();
                examineDimensionIndexDO.setIndexDimensionId(examineDimensionId);
                examineDimensionIndexDO.setExamineId(examineId);
                examineDimensionIndexDO.setIndexName(indexName);
                examineDimensionIndexDO.setIndexIllustrate(indexIllustrate);
                examineDimensionIndexDO.setIndexStandard(indexStandard);
                //todo 只有一种方式但是为了逻辑判断暂设置为 1
                examineDimensionIndexDO.setIndexType(1);
                examineDimensionIndexDO.setIndexWeight(indexWeight);
//                examineDimensionIndexDO.setUuId(examineDimensionIndex.getString("uuId"));
                examineDimensionIndexMapper.insert(examineDimensionIndexDO);
                int examineDimensionIndexId = examineDimensionIndexDO.getId();
            });
        });
        //考核评分流程
        List<JSONObject> examineFlowPaths = param.getList("examineFlowPaths", JSONObject.class);
        List<JSONObject> examineResults = param.getList("examineResults", JSONObject.class);
        List<JSONObject> resultConfigs = param.getList("resultConfigs", JSONObject.class);
        if (ObjectUtils.isEmpty(examineFlowPaths)) {
            throw exception(EXAMINE_FLOW_PATH_NOT_EXISTS);
        }

        ArrayList<JSONObject> examineFlowPerformanceList = JSONObject.parseObject(JSONObject.toJSONString(examineFlowPaths), new ArrayList<JSONObject>().getClass());
        examineFlowPerformanceList.forEach(examineFlowPath -> {
            //1部门负责人 2指定评分人
            Integer flowPathType = examineFlowPath.getInteger("flowPathType");
            //评分人id  部门负责人是不相同的 所以需要查询当前被评分的负责人
            Integer flowPathEmployeeId = examineFlowPath.getInteger("flowPathEmployeeId");
            // 1可见所有人评语评分2仅可见自己的
            Integer flowPathVisible = examineFlowPath.getInteger("flowPathVisible");
            //评语是否必填 1必填 2非必填
            Integer flowPathComment = examineFlowPath.getInteger("flowPathComment");
            //评分权重
            Integer flowPathWeight = examineFlowPath.getInteger("flowPathWeight");
            //评分人
            List<String> uuIdList = examineFlowPath.getList("uuIdList", String.class);
            if (flowPathType == 2) {
                EmployeeDO employeeDO = employeeMapper.selectById(flowPathEmployeeId.longValue());
                flowPathEmployeeId = employeeDO.getUserId().intValue();
            }
//            else {
//                adminUserApi.selectResponsibleUserIdByGradeDept()
//            }

            ExamineFlowPathDO examineFlowPathDO = new ExamineFlowPathDO();
            examineFlowPathDO.setExamineId(examineId);
            examineFlowPathDO.setFlowPathType(flowPathType);
            examineFlowPathDO.setFlowPathEmployeeId(flowPathEmployeeId);
            examineFlowPathDO.setFlowPathVisible(flowPathVisible);
            examineFlowPathDO.setFlowPathComment(flowPathComment);
            examineFlowPathDO.setFlowPathWeight(flowPathWeight);
            examineFlowPathMapper.insert(examineFlowPathDO);
            int examineFlowPathId = examineFlowPathDO.getId();
            uuIdList.forEach(uuId -> {
                ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectOne(new LambdaQueryWrapper<ExamineDimensionDO>() {{
                    eq(ExamineDimensionDO::getUuId, uuId);
                }});
                List<ExamineDimensionIndexDO> examineDimensionIndexDOs = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                    in(ExamineDimensionIndexDO::getIndexDimensionId, examineDimensionDO.getId());
                }});
                examineDimensionIndexDOs.forEach(examineDimensionIndexDO -> {
                    ExamineFlowPathIndexDo examineFlowPathIndexDo = new ExamineFlowPathIndexDo();
                    examineFlowPathIndexDo.setFlowPathId(examineFlowPathId);
                    examineFlowPathIndexDo.setIndexId(examineDimensionIndexDO.getId());
                    examineFlowPathIndexMapper.insert(examineFlowPathIndexDo);
                });

            });
        });
        ArrayList<JSONObject> examineResultList = JSONObject.parseObject(JSONObject.toJSONString(examineResults), new ArrayList<JSONObject>().getClass());
        examineResultList.forEach(examineResult -> {
            //审核层级
            Integer examineResultGrade = examineResult.getInteger("examineResultGrade");
            //审核结果类型 1部门负责人 2指定审核人
            Integer resultType = examineResult.getInteger("resultType");
            //制定审核人时审核人员id
            Integer resultEmployeeId = examineResult.getInteger("resultEmployeeId");
            if (resultType == 2) {
                EmployeeDO employeeDO = employeeMapper.selectById(resultEmployeeId);
                resultEmployeeId = employeeDO.getUserId().intValue();
            }
            ExamineReviewDO examineReviewDO = new ExamineReviewDO();
            examineReviewDO.setExamineResultGrade(examineResultGrade);
            examineReviewDO.setExamineId(examineId);
            examineReviewDO.setResultType(resultType);
            examineReviewDO.setResultEmployeeId(resultEmployeeId);
            examineReviewMapper.insert(examineReviewDO);
        });

        if (ObjectUtils.isEmpty(resultConfigs)) {
            throw exception(EXAMINE_RESULT_CONFIG_NOT_EXISTS);
        }
        ArrayList<JSONObject> resultConfigList = JSONObject.parseObject(JSONObject.toJSONString(resultConfigs), new ArrayList<JSONObject>().getClass());
        if (id != null) {
            List<ExamineResultConfigDO> examineResultConfigDOS = examineResultConfigMapper.selectList(new LambdaQueryWrapper<ExamineResultConfigDO>() {{
                eq(ExamineResultConfigDO::getExamineId, id);
            }});
            examineResultConfigDOS.forEach(examineResultConfigDO -> {
                examineResultConfigDO.setExamineId(examineId);
                examineResultConfigMapper.updateById(examineResultConfigDO);
            });
            resultConfigList.forEach(resultConfig -> {
                Integer configId = resultConfig.getInteger("id");
                if (configId == null) {
                    //等级名称
                    String gradeName = resultConfig.getString("gradeName");
                    //最小分数
                    Integer fractionStart = resultConfig.getInteger("fractionStart");
                    //最大分数
                    Integer fractionEnd = resultConfig.getInteger("fractionEnd");
                    ExamineResultConfigDO examineResultConfigDO = new ExamineResultConfigDO();
                    examineResultConfigDO.setExamineId(examineId);
                    examineResultConfigDO.setFractionEnd(fractionEnd);
                    examineResultConfigDO.setFractionStart(fractionStart);
                    examineResultConfigDO.setGradeName(gradeName);
                    examineResultConfigMapper.insert(examineResultConfigDO);
                } else {
                    //等级名称
                    String gradeName = resultConfig.getString("gradeName");
                    //最小分数
                    Integer fractionStart = resultConfig.getInteger("fractionStart");
                    //最大分数
                    Integer fractionEnd = resultConfig.getInteger("fractionEnd");
                    ExamineResultConfigDO examineResultConfigDO = new ExamineResultConfigDO();
                    examineResultConfigDO.setExamineId(examineId);
                    examineResultConfigDO.setFractionEnd(fractionEnd);
                    examineResultConfigDO.setFractionStart(fractionStart);
                    examineResultConfigDO.setGradeName(gradeName);
                    examineResultConfigDO.setId(configId);
                    examineResultConfigMapper.insert(examineResultConfigDO);
                }
            });
        } else {
            resultConfigList.forEach(resultConfig -> {
                //等级名称
                String gradeName = resultConfig.getString("gradeName");
                //最小分数
                Integer fractionStart = resultConfig.getInteger("fractionStart");
                //最大分数
                Integer fractionEnd = resultConfig.getInteger("fractionEnd");
                ExamineResultConfigDO examineResultConfigDO = new ExamineResultConfigDO();
                examineResultConfigDO.setExamineId(examineId);
                examineResultConfigDO.setFractionEnd(fractionEnd);
                examineResultConfigDO.setFractionStart(fractionStart);
                examineResultConfigDO.setGradeName(gradeName);
                examineResultConfigMapper.insert(examineResultConfigDO);
            });
        }
        return examineDO.getId();
    }


    //    public List<Integer> selectByAllEmployeeId(JSONArray children){
//    List<Integer> employeeIdList=new ArrayList<>();
//
//    children.forEach(c->{
//        JSONObject jsonObject = JSONObject.parseObject(c);
//        if(jsonObject.getInteger("type")==2){
//            employeeIdList.add(jsonObject.getInteger("id"));
//
//        }
//        if(!ObjectUtils.isEmpty(jsonObject.getJSONArray("children"))){
//            List<Integer> children1 = selectByAllEmployeeId(jsonObject.getJSONArray("children"));
//        }
//    });
//
//    return employeeIdList;
//
//    }
    @Override
    public Integer updateExamine(JSONObject param) {
        deletedExamineAll(param.getInteger("id"));
        Integer examineId = dataProcessing(param);
        List<ExamineFlowPerformanceDO> list = examineFlowPerformanceMapper.selectList(new LambdaQueryWrapper<ExamineFlowPerformanceDO>() {{
            eq(ExamineFlowPerformanceDO::getExamineId, param.getInteger("id"));
        }});
        list.forEach(l -> {
            l.setExamineId(examineId);
            examineFlowPerformanceMapper.updateById(l);
        });

        return examineId;
    }

    public void deletedExamineAll(Integer id) {

        examineMapper.delete(new LambdaQueryWrapper<ExamineDO>() {{
            eq(ExamineDO::getId, id);
        }});
        examineResultConfigMapper.delete(new LambdaQueryWrapper<ExamineResultConfigDO>() {{
            eq(ExamineResultConfigDO::getExamineId, id);
        }});
        examineAssessmentScopeMapper.delete(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getExamineId, id);
        }});
        examineDimensionMapper.delete(new LambdaQueryWrapper<ExamineDimensionDO>() {{
            eq(ExamineDimensionDO::getExamineId, id);
        }});
        examineDimensionIndexMapper.delete(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
            eq(ExamineDimensionIndexDO::getExamineId, id);
        }});

        List<Integer> collect = examineFlowPathMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
            eq(ExamineFlowPathDO::getExamineId, id);
        }}).stream().map(ExamineFlowPathDO::getId).collect(Collectors.toList());
        if (!ObjectUtils.isEmpty(collect)) {
            examineFlowPathIndexMapper.delete(new LambdaQueryWrapper<ExamineFlowPathIndexDo>() {{
                in(ExamineFlowPathIndexDo::getFlowPathId, collect);
            }});
        }


        examineFlowPathMapper.delete(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
            eq(ExamineFlowPathDO::getExamineId, id);
        }});
        examineReviewMapper.delete(new LambdaQueryWrapper<ExamineReviewDO>() {{
            eq(ExamineReviewDO::getExamineId, id);
        }});
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, id);
        }});
        examineFlowPerformanceMapper.delete(new LambdaQueryWrapper<ExamineFlowPerformanceDO>() {{
            eq(ExamineFlowPerformanceDO::getExamineId, id);
        }});
        if (!ObjectUtils.isEmpty(examineRecordRaterDOS)) {
            examineRecordRaterMapper.delete(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getExamineId, id);
            }});
            examineRecordRaterIndexMapper.delete(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
                in(ExamineRecordRaterIndexDO::getRaterId, examineRecordRaterDOS.stream().map(ExamineRecordRaterDO::getId).collect(Collectors.toList()));
            }});
        }
        examineRecordMapper.delete(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, id);
        }});
    }


    @Override
    public void deletedExamineRecordAll(Integer id, Integer examineId) {

        examineAssessmentScopeMapper.delete(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getEmployeeId, id);
            eq(ExamineAssessmentScopeDO::getExamineId, examineId);
        }});
    }

    @Override
    public void deleteExamine(Integer id) {
        deletedExamineAll(id);

    }

    private void validateExamineExists(Integer id) {
        if (examineMapper.selectById(id) == null) {
            throw exception(EXAMINE_NOT_EXISTS);
        }
    }

    @Override
    public ExamineDO getExamine(Integer id) {
        return examineMapper.selectById(id);
    }


    @Override
    public PageResult<ExamineDO> getExaminePage(ExaminePageReqVO pageReqVO) {
        return null;
    }

    @Override
    public JSONObject selectExamineVoList(Integer id) {
        ExamineDO examineDO = examineMapper.selectById(id);
        if (ObjectUtils.isEmpty(examineDO)) {
            throw exception(EXAMINE_DIMENSION_NOT_EXISTS);
        }
        JSONObject examineObject = new JSONObject();
        examineObject.put("examinePlanName", examineDO.getExaminePlanName());
        examineObject.put("cycleType", examineDO.getExamineCycleTypeId());
        examineObject.put("startTime", examineDO.getExamineCycleStart());
        examineObject.put("endTime", examineDO.getExamineCycleEnd());
        examineObject.put("illustrate", examineDO.getExamineIllustrate());
        examineObject.put("examineLimit", examineDO.getExamineLimit());
        examineObject.put("year", examineDO.getExamineYear());
        examineObject.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        examineObject.put("templateId", examineDO.getTemplateId());
        examineObject.put("templateName", examineDO.getTemplateName());
        examineObject.put("resultConfigTemplateId", examineDO.getResultConfigTemplateId());
        if (examineDO.getIsReview() == 1) {
            examineObject.put("value1", true);
        } else {
            examineObject.put("value1", false);
        }
        final List<ExamineAssessmentScopeDO> examineAssessmentScopeDOS = examineAssessmentScopeMapper.selectList(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getExamineId, id);
        }});
        List<JSONObject> assessmentScopeList = new ArrayList<>();
        List<Integer> employeeIds = new ArrayList<>();
        Set<Integer> postId = new HashSet<>();
        examineAssessmentScopeDOS.forEach(examineAssessmentScopeDO -> {
            if (examineAssessmentScopeDO.getAssessmentScopeType() == 1) {
                employeeIds.add(examineAssessmentScopeDO.getEmployeeId().intValue());
            } else {
                postId.add(examineAssessmentScopeDO.getAssessmentPostId());
            }
        });
        if (!ObjectUtils.isEmpty(employeeIds)) {
            JSONObject assessmentScope = new JSONObject();
            assessmentScope.put("assessmentScopeType", 1);
            assessmentScope.put("employeeIds", employeeIds);
            assessmentScopeList.add(assessmentScope);
        }
        if (!ObjectUtils.isEmpty(postId)) {
            JSONObject assessmentScope = new JSONObject();
            assessmentScope.put("assessmentScopeType", 2);
            assessmentScope.put("stationList", postId);
            assessmentScopeList.add(assessmentScope);
        }
        examineObject.put("assessmentScope", assessmentScopeList);
        List<JSONObject> examineDimensions = new ArrayList<>();
        final List<ExamineDimensionDO> examineDimensionDOS = examineDimensionMapper.selectList(new LambdaQueryWrapper<ExamineDimensionDO>() {{
            eq(ExamineDimensionDO::getExamineId, id);
        }});
        examineDimensionDOS.forEach(examineDimensionDO -> {
            JSONObject examineDimension = new JSONObject();
            examineDimension.put("dimensionName", examineDimensionDO.getDimensionName());
            examineDimension.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            examineDimension.put("dimensionRemark", examineDimensionDO.getDimensionRemark());
            examineDimension.put("uuId", examineDimensionDO.getUuId());
            List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                eq(ExamineDimensionIndexDO::getExamineId, id);
                eq(ExamineDimensionIndexDO::getIndexDimensionId, examineDimensionDO.getId());
            }});
            List<JSONObject> examineDimensionIndexList = new ArrayList<>();
            examineDimensionIndexDOS.forEach(examineDimensionIndexDO -> {
                JSONObject examineDimensionIndex = new JSONObject();
                //指标名称
                examineDimensionIndex.put("indexName", examineDimensionIndexDO.getIndexName());
                //指标说明
                examineDimensionIndex.put("indexIllustrate", examineDimensionIndexDO.getIndexIllustrate());
                //指标考核标准
                examineDimensionIndex.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
                //指标权重
                examineDimensionIndex.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
                //评分输入方式
                examineDimensionIndex.put("indexType", examineDimensionIndexDO.getIndexType());

//                examineDimensionIndex.put("uuId", examineDimensionIndexDO.getUuId());

                examineDimensionIndexList.add(examineDimensionIndex);
            });
            examineDimension.put("examineDimensionIndexs", examineDimensionIndexList);
            examineDimensions.add(examineDimension);
        });
        examineObject.put("examineDimensions", examineDimensions);
        List<JSONObject> examineFlowPaths = new ArrayList<>();
        final List<ExamineFlowPathDO> examineFlowPathDOS = examineFlowPathMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
            eq(ExamineFlowPathDO::getExamineId, id);
        }});
        examineFlowPathDOS.forEach(examineFlowPathDO -> {
            List<String> uuIdList = new ArrayList<>();
            JSONObject examineFlowPath = new JSONObject();
            //1部门负责人 2指定评分人
            examineFlowPath.put("flowPathType", examineFlowPathDO.getFlowPathType());

            if (examineFlowPathDO.getFlowPathType() == 1) {
                examineFlowPath.put("flowPathEmployeeId", examineFlowPathDO.getFlowPathEmployeeId());
            } else {
                //评分人id  部门负责人是不相同的 所以需要查询当前被评分的负责人
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineFlowPathDO.getFlowPathEmployeeId().longValue());
                examineFlowPath.put("flowPathEmployeeId", employeeDO.getId());
            }

            // 1可见所有人评语评分2仅可见自己的
            examineFlowPath.put("flowPathVisible", examineFlowPathDO.getFlowPathVisible());
            //评语是否必填 1必填 2非必填
            examineFlowPath.put("flowPathComment", examineFlowPathDO.getFlowPathComment());
            //评分权重
            examineFlowPath.put("flowPathWeight", examineFlowPathDO.getFlowPathWeight());
            List<ExamineFlowPathIndexDo> examineFlowPathIndexDos = examineFlowPathIndexMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathIndexDo>() {{
                eq(ExamineFlowPathIndexDo::getFlowPathId, examineFlowPathDO.getId());
            }});

            List<Integer> integerStream = examineFlowPathIndexDos.stream().map(e -> e.getIndexId()).collect(Collectors.toList());
            List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                in(!ObjectUtils.isEmpty(integerStream), ExamineDimensionIndexDO::getId, integerStream);
            }});
            Set<Integer> collect = examineDimensionIndexDOS.stream().map(e -> e.getIndexDimensionId()).collect(Collectors.toSet());
            collect.forEach(disId -> {

                ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(disId);
                uuIdList.add(examineDimensionDO.getUuId());
            });
            examineFlowPath.put("uuIdList", uuIdList);
            examineFlowPaths.add(examineFlowPath);
        });
        examineObject.put("examineFlowPaths", examineFlowPaths);
        List<JSONObject> examineResultList = new ArrayList<>();
        final List<ExamineReviewDO> examineReviewDOS = examineReviewMapper.selectList(new LambdaQueryWrapper<ExamineReviewDO>() {{
            eq(ExamineReviewDO::getExamineId, id);
        }});
        if (ObjectUtils.isEmpty(examineReviewDOS)) {

        } else {
            examineReviewDOS.forEach(examineReviewDO -> {
                JSONObject examineResult = new JSONObject();
                //审核层级
                examineResult.put("examineResultGrade", examineReviewDO.getExamineResultGrade());
                //审核结果类型 1部门负责人 2指定审核人
                examineResult.put("resultType", examineReviewDO.getResultType());
                if (examineReviewDO.getResultType() == 1) {
                    examineResult.put("resultEmployeeId", examineReviewDO.getResultEmployeeId());
                } else {
                    //制定审核人时审核人员id
                    EmployeeDO employeeDO = employeeMapper.selectByUserId(examineReviewDO.getResultEmployeeId().longValue());
                    examineResult.put("resultEmployeeId", employeeDO.getId());
                }
                examineResultList.add(examineResult);
            });
            examineObject.put("examineResults", examineResultList);
        }
        final List<ExamineResultConfigDO> examineResultConfigDOS = examineResultConfigMapper.selectList(new LambdaQueryWrapper<ExamineResultConfigDO>() {{
            eq(ExamineResultConfigDO::getExamineId, id);
        }});
        List<JSONObject> resultConfigs = new ArrayList<>();
        examineResultConfigDOS.forEach(examineResultConfigDO -> {
            JSONObject resultConfig = new JSONObject();
            //等级名称
            resultConfig.put("gradeName", examineResultConfigDO.getGradeName());
            //最小分数
            resultConfig.put("fractionStart", examineResultConfigDO.getFractionStart());
            //最大分数
            resultConfig.put("fractionEnd", examineResultConfigDO.getFractionEnd());
            resultConfig.put("id", examineResultConfigDO.getId());
            resultConfigs.add(resultConfig);
        });
        examineObject.put("resultConfigs", resultConfigs);
        return examineObject;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer startExamine(Integer id) {
        final ExamineDO examineDO = examineMapper.selectById(id);
        if (ObjectUtils.isEmpty(examineDO)) {
            throw exception(EXAMINE_NOT_EXISTS);
        }

        List<ExamineAssessmentScopeDO> examineAssessmentScopeDOS = examineAssessmentScopeMapper.selectList(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getExamineId, id);
        }});
        examineAssessmentScopeDOS.forEach(examineAssessmentScopeDO -> {
            EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                eq(EmployeeDO::getUserId, examineAssessmentScopeDO.getEmployeeId());
            }});
            if (ObjectUtils.isEmpty(employeeDO)) {
                throw exception(EXAMINE_EMPLOYEE_RETIRE);
            }
            if (examineAssessmentScopeDO.getAssessmentScopeType() == 2) {
                if (!examineAssessmentScopeDO.getAssessmentPostId().equals(employeeDO.getPostId())) {

                }
            }
            ExamineRecordDO examineRecordDO = new ExamineRecordDO();
            examineRecordDO.setExamineId(id);
            examineRecordDO.setEmployeeId(examineAssessmentScopeDO.getEmployeeId());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            examineRecordDO.setDepartmentId(examineAssessmentScopeDO.getAssessmentScopeId());
            if (!ObjectUtils.isEmpty(employeeDO) && !ObjectUtils.isEmpty(employeeDO.getPostId())) {
                examineRecordDO.setPostId(employeeDO.getPostId().intValue());
            }
            examineRecordDO.setExanineType(examineAssessmentScopeDO.getAssessmentScopeType());
            examineRecordDO.setExamineStatus(-1);
            examineRecordDO.setExamineGradeId(-1);
            examineRecordDO.setRatingScore(0);
            examineRecordDO.setExamineName(examineDO.getExaminePlanName());
            examineRecordDO.setCycleStartTime(examineDO.getExamineCycleStart());
            examineRecordDO.setCycleEndTime(examineDO.getExamineCycleEnd());
            examineRecordDO.setExamineYear(examineDO.getExamineYear());
            examineRecordDO.setCycleTimeType(examineDO.getExamineCycleTimeType());
            examineRecordDO.setCycleType(examineDO.getExamineCycleTypeId());
            examineRecordDO.setExamineIllustrate(examineRecordDO.getExamineIllustrate());
            examineRecordMapper.insert(examineRecordDO);
            //考核计划生成第一层表id
            Integer examineRecordId = examineRecordDO.getId();
            final List<ExamineFlowPathDO> examineFlowPathDOS = examineFlowPathMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
                eq(ExamineFlowPathDO::getExamineId, id);
            }});
            for (int i = 0; i < examineFlowPathDOS.size(); i++) {
                ExamineRecordRaterDO examineRecordRaterDO = new ExamineRecordRaterDO();
                examineRecordRaterDO.setExamineId(id);
                examineRecordRaterDO.setRaterStatus(-1);
                examineRecordRaterDO.setAppealReason("");
                examineRecordRaterDO.setRejectReason("");
                examineRecordRaterDO.setRaterComment("");
                examineRecordRaterDO.setScorerOrder(i);
                examineRecordRaterDO.setRecordId(examineRecordId);
                examineRecordRaterDO.setRaterWeight(examineFlowPathDOS.get(i).getFlowPathWeight());
                if (examineFlowPathDOS.get(i).getFlowPathType() == 1) {
                    EmployeeDO employeeCreateDO = employeeMapper.selectById(examineDO.getCreateBy());
                    CommonResult<Integer> integerCommonResult = adminUserApi.selectResponsibleUserIdByGradeDept(employeeCreateDO.getDeptId().intValue(), examineFlowPathDOS.get(i).getFlowPathEmployeeId());
                    if (integerCommonResult.getCheckedData() == 0) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_RETIRE);
                    } else if (integerCommonResult.getCheckedData() == -1) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_NOT_EXISTS);
                    }
                    examineRecordRaterDO.setRaterEmployeeId(integerCommonResult.getCheckedData());
                    //todo 调用Api接口有问题
//                    examineRecordRaterDO.setRaterEmployeeId(1);
                } else if (examineFlowPathDOS.get(i).getFlowPathType() == 2) {
                    examineRecordRaterDO.setRaterEmployeeId(examineFlowPathDOS.get(i).getFlowPathEmployeeId());
                }
                examineRecordRaterDO.setRaterGradeId(-1);
                examineRecordRaterDO.setCreateTime(DateUtil.currentDateFormat());
                examineRecordRaterMapper.insert(examineRecordRaterDO);
                int finalI = i;
                List<ExamineFlowPathIndexDo> examineFlowPathIndexDos = examineFlowPathIndexMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathIndexDo>() {{
                    eq(ExamineFlowPathIndexDo::getFlowPathId, examineFlowPathDOS.get(finalI).getId());
                }});
                List<Integer> indexIdList = examineFlowPathIndexDos.stream().map(ExamineFlowPathIndexDo::getIndexId).collect(Collectors.toList());
                List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                    eq(ExamineDimensionIndexDO::getExamineId, id);
                    in(ExamineDimensionIndexDO::getId, indexIdList);
                }});
                examineDimensionIndexDOS.forEach(examineDimensionIndexDO -> {
                    ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineDimensionIndexDO.getIndexDimensionId());
                    ExamineRecordRaterIndexDO examineRecordRaterIndexDO = new ExamineRecordRaterIndexDO();
                    examineRecordRaterIndexDO.setRecordId(examineRecordId);
                    examineRecordRaterIndexDO.setDimensionIndexId(examineDimensionIndexDO.getId());
                    examineRecordRaterIndexDO.setDimensionId(examineDimensionDO.getId());
                    examineRecordRaterIndexDO.setRaterId(examineRecordRaterDO.getId());
                    examineRecordRaterIndexDO.setRateComment("");
                    examineRecordRaterIndexMapper.insert(examineRecordRaterIndexDO);
                });
            }
            List<ExamineReviewDO> examineReviewDOS = examineReviewMapper.selectList(new LambdaQueryWrapper<ExamineReviewDO>() {{
                eq(ExamineReviewDO::getExamineId, id);
                orderByAsc(ExamineReviewDO::getExamineResultGrade);
            }});
            for (int i = 0; i < examineReviewDOS.size(); i++) {
                final EmployeeDO employeeReviewDO = employeeMapper.selectByUserId(examineReviewDOS.get(i).getResultEmployeeId().longValue());
                ExamineResultDO examineResultDO = new ExamineResultDO();
                examineResultDO.setExamineId(id);
                examineResultDO.setResultStatus(0);
                examineResultDO.setResultType(examineReviewDOS.get(i).getResultType());
                examineResultDO.setRecordId(examineRecordId);
                examineResultDO.setExamineResultGrade(examineReviewDOS.get(i).getExamineResultGrade());
                if (examineReviewDOS.get(i).getResultType() == 1) {
                    final CommonResult<Integer> integerCommonResult = adminUserApi.selectResponsibleUserIdByGradeDept(employeeDO.getDeptId().intValue(), examineReviewDOS.get(i).getResultEmployeeId());
                    if (integerCommonResult.getCheckedData() == 0) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_RETIRE);
                    } else if (integerCommonResult.getCheckedData() == -1) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_NOT_EXISTS);
                    }
                    examineResultDO.setResultEmployeeId(integerCommonResult.getCheckedData());
                } else if (examineReviewDOS.get(i).getResultType() == 2) {
                    examineResultDO.setResultEmployeeId(employeeReviewDO.getUserId().intValue());
                }
                examineResultMapper.insert(examineResultDO);
            }
        });

        examineDO.setExamineState(1);
        examineMapper.updateById(examineDO);


        startProgress(examineDO.getId());
        return 1;
    }

    @Override
    public JSONObject selectExamineEmployeeList(Integer examineId) {
        JSONObject resultData = new JSONObject();
        List<JSONObject> arrangeList = new ArrayList<>();
        List<ExamineAssessmentScopeDO> examineAssessmentScopeDOS = examineAssessmentScopeMapper.selectList(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getExamineId, examineId);
        }});
        List<JSONObject> result = new ArrayList<>();
        final List<ExamineFlowPathDO> examineFlowPathDOList = examineFlowPathMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
            eq(ExamineFlowPathDO::getExamineId, examineId);
        }});
        for (int i = 0; i < examineFlowPathDOList.size(); i++) {
            JSONObject arrange = new JSONObject();
            arrange.put("columnName", "folowPathName" + (i + 1));
            arrange.put("columnValue", "评分人" + (i + 1));
            arrangeList.add(arrange);
        }
        List<ExamineReviewDO> examineReviewList = examineReviewMapper.selectList(new LambdaQueryWrapper<ExamineReviewDO>() {{
            eq(ExamineReviewDO::getExamineId, examineId);
        }});
        for (int i = 0; i < examineReviewList.size(); i++) {
            JSONObject arrange = new JSONObject();
            arrange.put("columnName", "resultEmployee" + (i + 1));
            arrange.put("columnValue", "审核人员" + (i + 1));
            arrangeList.add(arrange);
        }
        for (int j = 0; j < examineAssessmentScopeDOS.size(); j++) {
            JSONObject jsonObject = new JSONObject();
            int finalJ = j;
            EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                eq(EmployeeDO::getUserId, examineAssessmentScopeDOS.get(finalJ).getEmployeeId());
            }});
            final CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            jsonObject.put("employeeName", employeeDO.getName());
            jsonObject.put("employeeNo", employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                jsonObject.put("deptName", dept.getCheckedData().getName());
            } else {
                jsonObject.put("deptName", "");
            }

            jsonObject.put("employeeId", employeeDO.getUserId());
            final List<ExamineFlowPathDO> examineFlowPathDOS = examineFlowPathMapper.selectList(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
                eq(ExamineFlowPathDO::getExamineId, examineId);
            }});
            for (int i = 0; i < examineFlowPathDOS.size(); i++) {
                if (examineFlowPathDOS.get(i).getFlowPathType() == 1) {
                    CommonResult<Integer> integerCommonResult = adminUserApi.selectResponsibleUserIdByGradeDept(employeeDO.getDeptId().intValue(), examineFlowPathDOS.get(i).getFlowPathEmployeeId());
                    if (integerCommonResult.getCheckedData() == 0) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_RETIRE);
                    } else if (integerCommonResult.getCheckedData() == -1) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_NOT_EXISTS);
                    }
                    EmployeeDO employeeDOPF = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                        eq(EmployeeDO::getUserId, integerCommonResult.getCheckedData().intValue());
                    }});
                    jsonObject.put("folowPathName" + (i + 1), employeeDOPF.getName());
                } else if (examineFlowPathDOS.get(i).getFlowPathType() == 2) {
                    int finalI = i;
                    EmployeeDO employeeDOPF = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                        eq(EmployeeDO::getUserId, examineFlowPathDOS.get(finalI).getFlowPathEmployeeId());
                    }});
                    jsonObject.put("folowPathName" + (i + 1), employeeDOPF.getName());
                }
            }
            List<ExamineReviewDO> examineReviewDOS = examineReviewMapper.selectList(new LambdaQueryWrapper<ExamineReviewDO>() {{
                eq(ExamineReviewDO::getExamineId, examineId);
            }});


            for (int i = 0; i < examineReviewDOS.size(); i++) {
                if (examineReviewDOS.get(i).getResultType() == 1) {
                    final CommonResult<Integer> integerCommonResult = adminUserApi.selectResponsibleUserIdByGradeDept(employeeDO.getDeptId().intValue(), examineReviewDOS.get(i).getResultEmployeeId());
                    if (integerCommonResult.getCheckedData() == 0) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_RETIRE);
                    } else if (integerCommonResult.getCheckedData() == -1) {
                        throw exception(EXAMINE_SCORE_EMPLOYEE_NOT_EXISTS);
                    }
//                examineReviewDO.setReviewEmployeeId(integerCommonResult.getCheckedData());
                    //todo 同上
                    JSONObject arrange = new JSONObject();
                    EmployeeDO employeeDOPF = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                        eq(EmployeeDO::getUserId, integerCommonResult.getCheckedData());
                    }});
                    jsonObject.put("resultEmployee" + (i + 1), employeeDOPF.getName());


                } else if (examineReviewDOS.get(i).getResultType() == 2) {
                    int finalI = i;
                    EmployeeDO employeeDOPF = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
                        eq(EmployeeDO::getUserId, examineReviewDOS.get(finalI).getResultEmployeeId());
                    }});
//                    examineReviewDO.setReviewEmployeeId(examineResultDOS.get(i).getResultEmployeeId());
                    jsonObject.put("resultEmployee" + (i + 1), employeeDOPF.getName());

                }

            }
            jsonObject.put("resultConfirm", employeeDO.getName());
            result.add(jsonObject);
        }
        ;
        resultData.put("data", result);
        resultData.put("folowPathName", arrangeList);
        return resultData;
    }

    @Override
    public ExamineDO selectExamine(Integer examineId) {
        ExamineDO examineDO = examineMapper.selectById(examineId);
        JSONObject jsonObject = new JSONObject();
        List<ExamineAssessmentScopeDO> examineAssessmentScopeDOS = examineAssessmentScopeMapper.selectList(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
            eq(ExamineAssessmentScopeDO::getExamineId, examineDO.getId());
        }});
        //部门
        final List<Long> departmentIdList = examineAssessmentScopeDOS.stream().filter(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeType() == 1).map(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeId().longValue()).collect(Collectors.toList());
        //岗位
        List<Long> postIdList = examineAssessmentScopeDOS.stream().filter(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeType() == 2).map(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeId().longValue()).collect(Collectors.toList());
        CommonResult<List<DeptRespDTO>> deptList = deptApi.getDeptList(departmentIdList);
        CommonResult<List<PostRespDTO>> allPostBatchIds = postApi.getAllPostBatchIds(postIdList);
        String deptNameList = deptList.getCheckedData().stream().map(DeptRespDTO::getName).collect(Collectors.joining(","));
        String postNameList = allPostBatchIds.getCheckedData().stream().map(PostRespDTO::getName).collect(Collectors.joining(","));

        examineDO.setDeptStr(deptNameList);
        examineDO.setPostStr(postNameList);

        return examineDO;
    }

    @Override
    public PageResult<ExamineDO> getExamineStartedList(Integer type, Integer pageNum, Integer pageSize, String examinePlanName, Integer examineCycleTypeId) {
        PageResult<ExamineDO> examineDOList = null;
        if (type == 1) {
            ExaminePageReqVO examinePageReqVO = new ExaminePageReqVO();
            examinePageReqVO.setPageNo(pageNum);
            examinePageReqVO.setPageSize(pageSize);
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            examineDOList = examineMapper.selectPage(param, new LambdaQueryWrapperX<ExamineDO>() {{
                ne(ExamineDO::getExamineState, 8);
                ne(ExamineDO::getExamineState, 9);
                ne(ExamineDO::getExamineState, -1);
                eq(!ObjectUtils.isEmpty(examineCycleTypeId), ExamineDO::getExamineCycleTypeId, examineCycleTypeId);
                like(!ObjectUtils.isEmpty(examinePlanName), ExamineDO::getExaminePlanName, examinePlanName);
                orderByDesc(ExamineDO::getId);
            }});
            if (!ObjectUtils.isEmpty(examineDOList)) {
                examineDOList.getList().forEach(examineDO -> {
                    List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                        eq(ExamineRecordRaterDO::getExamineId, examineDO.getId());
                    }});
                    long count = examineRecordRaterDOS.stream().filter(e -> e.getRaterStatus() == -1).count();
                    if (count == examineRecordRaterDOS.size()) {
                        examineDO.setExamineState(-1);
                    }
                });

            }
        } else if (type == 2) {
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            examineDOList = examineMapper.selectPage(param, new LambdaQueryWrapper<ExamineDO>() {{
                eq(ExamineDO::getExamineState, 8);
                eq(!ObjectUtils.isEmpty(examineCycleTypeId), ExamineDO::getExamineCycleTypeId, examineCycleTypeId);
                like(!ObjectUtils.isEmpty(examinePlanName), ExamineDO::getExaminePlanName, examinePlanName);
                orderByDesc(ExamineDO::getId);

            }});
        } else if (type == 3) {
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            examineDOList = examineMapper.selectPage(param, new LambdaQueryWrapper<ExamineDO>() {{
                eq(ExamineDO::getExamineState, 9);
                eq(!ObjectUtils.isEmpty(examineCycleTypeId), ExamineDO::getExamineCycleTypeId, examineCycleTypeId);
                like(!ObjectUtils.isEmpty(examinePlanName), ExamineDO::getExaminePlanName, examinePlanName);
                orderByDesc(ExamineDO::getId);
            }});
        } else if (type == 0) {
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            examineDOList = examineMapper.selectPage(param, new LambdaQueryWrapper<ExamineDO>() {{
                eq(ExamineDO::getExamineState, -1);
                eq(!ObjectUtils.isEmpty(examineCycleTypeId), ExamineDO::getExamineCycleTypeId, examineCycleTypeId);
                like(!ObjectUtils.isEmpty(examinePlanName), ExamineDO::getExaminePlanName, examinePlanName);
                orderByDesc(ExamineDO::getId);
            }});
        }
        if (examineDOList.getList() == null) {
            return new PageResult<>();
        }
        for (ExamineDO examineDO : examineDOList.getList()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("examineName", examineDO.getExaminePlanName());
            List<ExamineAssessmentScopeDO> examineAssessmentScopeDOS = examineAssessmentScopeMapper.selectList(new LambdaQueryWrapper<ExamineAssessmentScopeDO>() {{
                eq(ExamineAssessmentScopeDO::getExamineId, examineDO.getId());
            }});
            if (examineDO.getExamineCycleTypeId() == 3 || examineDO.getExamineCycleTypeId() == 4 || examineDO.getExamineCycleTypeId() == 5) {
                examineDO.setExamineCycleTimeType(examineDO.getExamineYear() + "年");
            } else if (examineDO.getExamineCycleTypeId() == 6) {
                examineDO.setExamineCycleTimeType(examineDO.getExamineCycleStart() + "-" + examineDO.getExamineCycleEnd());
            } else if (examineDO.getExamineCycleTypeId() == 2) {
                if (examineDO.getExamineCycleTimeType().equals("1")) {
                    examineDO.setExamineCycleTimeType(examineDO.getExamineYear() + "年" + "第一季度");
                } else if (examineDO.getExamineCycleTimeType().equals("2")) {
                    examineDO.setExamineCycleTimeType(examineDO.getExamineYear() + "年" + "第二季度");
                } else if (examineDO.getExamineCycleTimeType().equals("3")) {
                    examineDO.setExamineCycleTimeType(examineDO.getExamineYear() + "年" + "第三季度");
                } else if (examineDO.getExamineCycleTimeType().equals("4")) {
                    examineDO.setExamineCycleTimeType(examineDO.getExamineYear() + "年" + "第四季度");
                }

                examineDO.setExamineCycleTimeType(examineDO.getExamineCycleTimeType());
            } else {
                examineDO.setExamineCycleTimeType(examineDO.getExamineCycleTimeType());
            }
            //部门
            final List<Long> departmentIdList = examineAssessmentScopeDOS.stream().filter(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeType() == 1).map(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeId().longValue()).collect(Collectors.toList());
            //岗位
            final List<Long> postIdList = examineAssessmentScopeDOS.stream().filter(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeType() == 2).map(examineAssessmentScopeDO -> examineAssessmentScopeDO.getAssessmentScopeId().longValue()).collect(Collectors.toList());
            final CommonResult<List<DeptRespDTO>> deptList = deptApi.getDeptList(departmentIdList);
            final CommonResult<List<PostRespDTO>> allPostBatchIds = postApi.getAllPostBatchIds(postIdList);
            final String deptNameList = deptList.getCheckedData().stream().map(DeptRespDTO::getName).collect(Collectors.joining(","));
            final String postNameList = allPostBatchIds.getCheckedData().stream().map(PostRespDTO::getName).collect(Collectors.joining(","));
            jsonObject.put("examineName", examineDO.getExaminePlanName());
            if (!ObjectUtils.isEmpty(deptList)) {
                jsonObject.put("deptList", deptNameList);
                examineDO.setDeptStr(deptNameList);
            }
            if (!ObjectUtils.isEmpty(postNameList)) {
                examineDO.setPostStr(postNameList);
            }
            final Long employeeNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
//                notIn(ExamineRecordDO::getExamineStatus, 8, 9);
            }});
            final Long progressNUm = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                lt(ExamineRecordDO::getExamineStatus, 1);
            }});
            final Long scoreNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                in(ExamineRecordDO::getExamineStatus, 1, 2, 7, 8);
            }});
            final Long reviewNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                in(ExamineRecordDO::getExamineStatus, 3, 4);
            }});
            final Long resultNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                in(ExamineRecordDO::getExamineStatus, 5, 6);
            }});
            final Long examinePlanNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                in(ExamineRecordDO::getIsLowPerformance, 1);
            }});


            Long scoreStatus = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                ne(ExamineRecordDO::getExamineStatus, 2);
            }});
            Long reviewStatus = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                ne(ExamineRecordDO::getExamineStatus, 4);
            }});

            Long resultStatus = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineDO.getId());
                ne(ExamineRecordDO::getExamineStatus, 6);
            }});

            examineDO.setReviewStatus(reviewStatus);
            examineDO.setScoreStatus(scoreStatus);
            examineDO.setResultStatus(resultStatus);
            examineDO.setExaminePlanNum(examinePlanNum.intValue());
            examineDO.setResultNum(resultNum.intValue());
            examineDO.setEmployeeNum(employeeNum.intValue());
            examineDO.setProgressNum(progressNUm.intValue());
            examineDO.setScoreNum(scoreNum.intValue());
            examineDO.setReviewNum(reviewNum.intValue());
        }
        return examineDOList;
    }

    @Override
    public Integer startProgress(Integer examineId) {
        final ExamineDO examineDO = examineMapper.selectById(examineId);
        examineDO.setExamineState(1);
        examineMapper.updateById(examineDO);
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineDO.getId());
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            examineRecordDO.setExamineStatus(1);
            examineRecordMapper.updateById(examineRecordDO);
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                orderByAsc(ExamineRecordRaterDO::getScorerOrder);
                last("limit 1");
            }});
            examineRecordRaterDO.setRaterStatus(0);
            examineRecordRaterMapper.updateById(examineRecordRaterDO);
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效待评分，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordRaterDO.getRaterEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.SCORE.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
            JSONObject options = new JSONObject();
            options.put("examineId", examineRecordDO.getExamineId());
            options.put("recordEmployeeId", examineRecordDO.getEmployeeId());
            options.put("id", examineRecordRaterDO.getRecordId());
            options.put("scorerOrder", examineRecordRaterDO.getScorerOrder());
            options.put("type", 1);
            options.put("recordId", examineRecordRaterDO.getRecordId());
            singleToUserReqDTO.setOptions(options.toJSONString());
            singleToUserReqDTO.setType(1);
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        });

        return 1;
    }

    @Override
    @DataPermission(enable = false)
    public PageResult<ExamineRecordRaterDO> selectExamineBYEmployeeId(Integer type, Integer pageNum, Integer pageSize) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        List<Integer> integers = selectExamineIdListByLoginId(loginId.intValue(), type);
        if (ObjectUtils.isEmpty(integers)) {
            return new PageResult<>();
        }
        PageResult<ExamineRecordRaterDO> examineRecordRaterList = null;
        if (type == 1) {
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            Long l = examineRecordRaterMapper.selectCount(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                in(ExamineRecordRaterDO::getExamineId, integers);

            }});
            if (l <= 0) {
                return new PageResult<>();
            }
            examineRecordRaterList = examineRecordRaterMapper.selectPage(param, new LambdaQueryWrapperX<ExamineRecordRaterDO>() {{
                in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                in(ExamineRecordRaterDO::getExamineId, integers);
                orderByDesc(ExamineRecordRaterDO::getExamineId);
            }});
        } else if (type == 2) {
            Long l = examineRecordRaterMapper.selectCount(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRaterStatus, 1);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                in(ExamineRecordRaterDO::getExamineId, integers);
                orderByDesc(ExamineRecordRaterDO::getExamineId);
            }});
            if (l <= 0) {
                return new PageResult<>();
            }
            PageParam param = new PageParam();
            param.setPageNo(pageNum);
            param.setPageSize(pageSize);
            examineRecordRaterList = examineRecordRaterMapper.selectPage(param, new LambdaQueryWrapperX<ExamineRecordRaterDO>() {{
                in(ExamineRecordRaterDO::getRaterStatus, 1);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                in(ExamineRecordRaterDO::getExamineId, integers);
                orderByDesc(ExamineRecordRaterDO::getExamineId);
            }});
        }

        if (examineRecordRaterList.getList() == null) {
            return new PageResult<>();
        }
        examineRecordRaterList.getList().forEach(examineRecordRater -> {
            Integer recordId = examineRecordRater.getRecordId();
            ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
            BeanUtils.copyProperties(examineRecordDO, examineRecordRater);
            String employeeName = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue()).getName();
            String gradeName = null;
            ExamineDO examineDO = examineMapper.selectById(examineRecordRater.getExamineId());
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
//              eq(ExamineRecordRaterDO::getRaterStatus,0);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
            }});
            examineRecordRater.setExaminePlanName(examineDO.getExaminePlanName());
            examineRecordRater.setFraction(examineRecordRaterDO.getRaterFraction());
            if (examineRecordRaterDO.getRaterGradeId() != -1) {
                val examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                gradeName = examineResultConfigDO.getGradeName();
            }
            examineRecordRater.setGradeName(gradeName);
            examineRecordRater.setEmployeeName(employeeName);
            examineRecordRater.setRaterEmployeeId(examineRecordDO.getEmployeeId());
            examineRecordRater.setLoginId(loginId.intValue());
        });
        return examineRecordRaterList;
    }

    @Override
    @DataPermission(enable = false)
    public PageResult<ExamineRecordDO> selectExamineBYReviewEmployeeId(Integer type, Integer pageNum, Integer pageSize) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        PageParam param = new PageParam();
        param.setPageNo(pageNum);
        param.setPageSize(pageSize);
        if (type == 3) {
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getResultEmployeeId, loginId);
                eq(ExamineResultDO::getResultStatus, 1);
                orderByDesc(ExamineResultDO::getExamineId);
            }});
            if (ObjectUtils.isEmpty(examineResultDOS)) {
                return new PageResult<>();
            }
            List<Integer> collect = examineResultDOS.stream().map(ExamineResultDO::getRecordId).collect(Collectors.toList());

            PageResult<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectPage(param, new LambdaQueryWrapper<ExamineRecordDO>() {{
                in(ExamineRecordDO::getId, collect);
                orderByDesc(ExamineRecordDO::getExamineId);
            }});
            examineRecordDOS.getList().forEach(examineRecordDO -> {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setEmployeeName(employeeDO.getName());
                examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
                examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
                examineRecordDO.setFraction(examineRecordDO.getRatingScore());
                if (examineRecordDO.getExamineGradeId() != -1) {
                    ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                    examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
                }
            });
            return examineRecordDOS;
        }
        if (type == 4) {
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getResultEmployeeId, loginId);
                eq(ExamineResultDO::getResultStatus, 2);
                orderByDesc(ExamineResultDO::getExamineId);
            }});
            if (ObjectUtils.isEmpty(examineResultDOS)) {
                return new PageResult<>();
            }
            List<Integer> collect = examineResultDOS.stream().map(ExamineResultDO::getRecordId).collect(Collectors.toList());
            PageResult<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectPage(param, new LambdaQueryWrapper<ExamineRecordDO>() {{
                in(ExamineRecordDO::getId, collect);
                eq(ExamineRecordDO::getExamineStatus, 4);
                orderByDesc(ExamineRecordDO::getExamineId);
            }});
            examineRecordDOS.getList().forEach(examineRecordDO -> {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setEmployeeName(employeeDO.getName());
                examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
                examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
                examineRecordDO.setFraction(examineRecordDO.getRatingScore());
                if (examineRecordDO.getExamineGradeId() != -1) {
                    ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                    examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
                }
            });
            return examineRecordDOS;

        }
        if (type == 5) {
            EmployeeDO employeeDO1 = employeeMapper.selectById(loginId);
            PageResult<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectPage(param, new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getEmployeeId, loginId);
                eq(ExamineRecordDO::getExamineStatus, 5);
                orderByDesc(ExamineRecordDO::getExamineId);
            }});
            examineRecordDOS.getList().forEach(examineRecordDO -> {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setEmployeeName(employeeDO.getName());
                examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
                examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
                examineRecordDO.setFraction(examineRecordDO.getRatingScore());
                if (examineRecordDO.getExamineGradeId() != -1) {
                    ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                    examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
                }
            });
            return examineRecordDOS;
        }
        if (type == 6) {
            PageResult<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectPage(param, new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getEmployeeId, loginId);
                eq(ExamineRecordDO::getExamineStatus, 6);
                orderByDesc(ExamineRecordDO::getExamineId);
            }});
            examineRecordDOS.getList().forEach(examineRecordDO -> {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setEmployeeName(employeeDO.getName());
                examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
                examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
                examineRecordDO.setFraction(examineRecordDO.getRatingScore());
                if (examineRecordDO.getExamineGradeId() != -1) {
                    ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                    examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
                }
            });
            return examineRecordDOS;
        }
        return new PageResult<>();
    }

    @Override
    @DataPermission(enable = false)
    public JSONObject progress(Integer recordId, Integer scorerOrder) {
        String gradeName = null;
        int loginId = SecurityFrameworkUtils.getLoginUserId().intValue();
        JSONObject resultData = new JSONObject();
        List<JSONObject> resultList = new ArrayList<>();
        ExamineRecordRaterDO examineRecordDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getRecordId, recordId);
            eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
            orderByDesc(ExamineRecordRaterDO::getExamineId);
        }});
        ExamineFlowPathDO examineFlowPathDO = examineFlowPathMapper.selectOne(new LambdaQueryWrapper<ExamineFlowPathDO>() {{
            eq(ExamineFlowPathDO::getExamineId, examineRecordDO.getExamineId());
            eq(ExamineFlowPathDO::getFlowPathEmployeeId, loginId);
            last("limit 1");
        }});
        if (examineRecordDO.getRaterGradeId() != -1) {
            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getRaterGradeId());
            gradeName = examineResultConfigDO.getGradeName();
        }
        EmployeeDO employeeLogin = employeeMapper.selectByUserId(Integer.toUnsignedLong(loginId));
        resultData.put("employeeName", employeeLogin.getName());
        resultData.put("raterFraction", examineRecordDO.getRaterFraction());
        resultData.put("raterGrade", gradeName);
        resultData.put("appealReason", examineRecordDO.getAppealReason());
        resultData.put("rejectReason", examineRecordDO.getRejectReason());
        resultData.put("raterStatus", examineRecordDO.getRaterStatus());
        resultData.put("recordRaterId", examineRecordDO.getRecordId());
        if (!ObjectUtils.isEmpty(examineFlowPathDO)) {
            resultData.put("flowPathVisible", examineFlowPathDO.getFlowPathVisible());
            resultData.put("flowPathComment", examineFlowPathDO.getFlowPathComment());
        } else {
            resultData.put("flowPathComment", 1);
            resultData.put("flowPathVisible", 1);
        }

        List<ExamineRecordRaterDO> examineRecordDOList = new ArrayList<>();
        if (examineFlowPathDO.getFlowPathVisible() == 2) {

            examineRecordDOList = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                le(ExamineRecordRaterDO::getScorerOrder, scorerOrder);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
            }});
        } else {
            examineRecordDOList = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                le(ExamineRecordRaterDO::getScorerOrder, scorerOrder);
            }});
        }


        List<Integer> collect = examineRecordDOList.stream().map(ExamineRecordRaterDO::getId).collect(Collectors.toList());

        List<ExamineRecordRaterIndexDO> examineRecordRaterIndexDOS = examineRecordRaterIndexMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
            in(ExamineRecordRaterIndexDO::getRaterId, collect);
            orderByAsc(ExamineRecordRaterIndexDO::getDimensionIndexId);
        }});
        examineRecordRaterIndexDOS.forEach(examineRecordRaterIndexDO -> {
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineRecordRaterIndexDO.getDimensionId());
            ExamineDimensionIndexDO examineDimensionIndexDO = examineDimensionIndexMapper.selectById(examineRecordRaterIndexDO.getDimensionIndexId());
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectById(examineRecordRaterIndexDO.getRaterId());
            EmployeeDO employeeDO1 = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject result = new JSONObject();
            result.put("id", examineRecordRaterIndexDO.getId());
            result.put("dimensionName", examineDimensionDO.getDimensionName());
            result.put("dimensionIndexName", examineDimensionIndexDO.getIndexName());
            result.put("dimensionIllustrateName", examineDimensionIndexDO.getIndexIllustrate());
            result.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
            result.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
            result.put("employeeName", employeeDO1.getName());
            result.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            result.put("fraction", examineRecordRaterIndexDO.getFraction());
            result.put("rateComment", examineRecordRaterIndexDO.getRateComment());
            result.put("fractionEmployeeId", examineRecordRaterDO.getRaterEmployeeId());
            resultList.add(result);
        });
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordDOList.forEach(examineRecordRaterDO -> {

            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            commentResult.put("commentEmployeeId", examineRecordRaterDO.getRaterEmployeeId());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);
        });
        resultData.put("commentResultList", commentResultList);
        resultData.put("list", resultList);
        resultData.put("loginId", loginId);
        return resultData;
    }


    @Override
    public Integer startProgress(JSONObject param) {
        Integer oldStatus = 0;
        int loginId = SecurityFrameworkUtils.getLoginUserId().intValue();


        BigDecimal zfraction = BigDecimal.ZERO;
        List<HashMap> commentResultList = param.getList("commentResultList", HashMap.class);
        String raterComment = param.getString("raterComment");
        raterComment = commentResultList.stream().filter(jsonObject -> jsonObject.get("commentEmployeeId").equals(loginId)).map(jsonObject -> jsonObject.get("comment")).collect(Collectors.toList()).get(0).toString();
        Integer recordRaterId = param.getInteger("recordRaterId");
        ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getRecordId, recordRaterId);
            eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
        }});
        examineRecordRaterDO.setRaterComment(raterComment);
        List<Map> list = param.getList("list", Map.class).stream().filter(l -> l.get("fractionEmployeeId").equals(loginId)).collect(Collectors.toList());

        for (int i = 0; i < list.size(); i++) {
            Integer id = MapUtils.getInteger(list.get(i), "id");
            String rateComment = MapUtils.getString(list.get(i), "rateComment");
            Integer fraction = MapUtils.getInteger(list.get(i), "fraction");
            ExamineRecordRaterIndexDO examineRecordRaterIndexDO = examineRecordRaterIndexMapper.selectById(id);
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineRecordRaterIndexDO.getDimensionId());
            ExamineDimensionIndexDO examineDimensionIndexDO = examineDimensionIndexMapper.selectById(examineRecordRaterIndexDO.getDimensionIndexId());
            ExamineDO examineDO = examineMapper.selectById(examineDimensionDO.getExamineId());
            if (fraction > examineDO.getExamineLimit()) {
                throw exception(RATING_EXCEEDS_UPPER_LIMIT);
            }
            examineRecordRaterIndexDO.setFraction(fraction);
            examineRecordRaterIndexDO.setRateComment(rateComment);
            examineRecordRaterIndexMapper.updateById(examineRecordRaterIndexDO);
            zfraction = zfraction.add(
                    (new BigDecimal(fraction *
                            examineDimensionIndexDO.getIndexWeight() *
                            examineDimensionDO.getDimensionWeight())
                    ));

        }
        List<ExamineResultConfigDO> examineResultConfigDOS = examineResultConfigMapper.selectList(new LambdaQueryWrapper<ExamineResultConfigDO>() {{
            eq(ExamineResultConfigDO::getExamineId, examineRecordRaterDO.getExamineId());
        }});

        zfraction = zfraction.divide(new BigDecimal(10000)).setScale(0, BigDecimal.ROUND_HALF_UP);
        for (int i1 = 0; i1 < examineResultConfigDOS.size(); i1++) {
            Integer fractionStart = examineResultConfigDOS.get(i1).getFractionStart();
            Integer fractionEnd = examineResultConfigDOS.get(i1).getFractionEnd();
            if (zfraction.compareTo(new BigDecimal(fractionStart)) >= 0 && zfraction.compareTo(new BigDecimal(fractionEnd)) <= 0) {
                examineRecordRaterDO.setRaterGradeId(examineResultConfigDOS.get(i1).getId());
            }
        }
        examineRecordRaterDO.setRaterComment(raterComment);
        examineRecordRaterDO.setRaterFraction(zfraction.intValue());
        examineRecordRaterDO.setRaterStatus(1);
        if (examineRecordRaterDO.getRaterGradeId() == -1) {
            throw exception(RESULT_CONFIG_NOT_EXISTS);
        }
        examineRecordRaterMapper.updateById(examineRecordRaterDO);
        ExamineRecordRaterDO examineRecordRaterDOSuperior = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            le(ExamineRecordRaterDO::getRaterStatus, 0);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordRaterDO.getRecordId());
//            gt(ExamineRecordRaterDO::getScorerOrder, examineRecordRaterDO.getScorerOrder());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
            last("limit 1");
        }});
        if (ObjectUtils.isEmpty(examineRecordRaterDOSuperior)) {
            Double pfEmployeeScoreCount = 0.00;
            ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(examineRecordRaterDO.getRecordId());
            Integer examineStatus = examineRecordDO.getExamineStatus();
            oldStatus = examineStatus;
            examineRecordDO.setExamineStatus(2);
            examineRecordMapper.updateById(examineRecordDO);
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            }});
            for (int i = 0; i < examineRecordRaterDOS.size(); i++) {
                pfEmployeeScoreCount += examineRecordRaterDOS.get(i).getRaterFraction() * (examineRecordRaterDOS.get(i).getRaterWeight().doubleValue() / 100);
            }
            for (int i1 = 0; i1 < examineResultConfigDOS.size(); i1++) {
                Integer fractionStart = examineResultConfigDOS.get(i1).getFractionStart();
                Integer fractionEnd = examineResultConfigDOS.get(i1).getFractionEnd();
                if (pfEmployeeScoreCount >= fractionStart && pfEmployeeScoreCount <= fractionEnd) {
                    examineRecordDO.setGradeName(examineResultConfigDOS.get(i1).getGradeName());
                    examineRecordDO.setExamineGradeId(examineResultConfigDOS.get(i1).getId());
                }
            }
            examineRecordDO.setRatingScore(pfEmployeeScoreCount.intValue());
            examineRecordMapper.updateById(examineRecordDO);
            if (examineDO.getIsReview() == 1) {
                if (oldStatus == 8) {
                    startReview(examineRecordRaterDO.getExamineId());
                }

            } else {
                examineRecordDO.setExamineStatus(4);
                examineRecordMapper.updateById(examineRecordDO);
                examineRecordRaterDOS.forEach(examineRecordRaterDO1 -> {
                    examineRecordRaterDO1.setRaterStatus(3);
                    examineRecordRaterMapper.updateById(examineRecordRaterDO1);
                });
            }
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您提交的绩效评分已完成，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineDO.getCreateBy()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.RATING_COMPLETED.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_REVIEW.getCode());
            JSONObject options = new JSONObject();
            options.put("actives", 1);
            options.put("examineId", examineDO.getId());
            options.put("status", 2);
            options.put("type", 2);
            singleToUserReqDTO.setType(2);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        } else {
            examineRecordRaterDOSuperior.setRaterStatus(0);
            examineRecordRaterMapper.updateById(examineRecordRaterDOSuperior);
            ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(examineRecordRaterDOSuperior.getRecordId());
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效待评分，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordRaterDOSuperior.getRaterEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.SCORE.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
            JSONObject options = new JSONObject();
            options.put("examineId", examineRecordRaterDOSuperior.getExamineId());
            options.put("recordEmployeeId", examineRecordDO.getEmployeeId());
            options.put("id", examineRecordRaterDOSuperior.getRecordId());
            options.put("scorerOrder", examineRecordRaterDOSuperior.getScorerOrder());
            options.put("type", 1);
            options.put("recordId", examineRecordRaterDOSuperior.getRecordId());
            singleToUserReqDTO.setType(1);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        }
        return 1;
    }

    @Override
    public JSONObject checkProgress(List<JSONObject> param) {
        BigDecimal count = BigDecimal.ZERO;
        Integer examineId = examineDimensionMapper.selectById(examineRecordRaterIndexMapper.selectById(param.get(0).getInteger("id")).getDimensionId()).getExamineId();
        JSONObject result = new JSONObject();
        for (int i = 0; i < param.size(); i++) {
            Integer rateId = param.get(i).getInteger("id");
            ExamineRecordRaterIndexDO examineRecordRaterIndexDO = examineRecordRaterIndexMapper.selectById(rateId);
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineRecordRaterIndexDO.getDimensionId());
            ExamineDimensionIndexDO examineDimensionIndexDO = examineDimensionIndexMapper.selectById(examineRecordRaterIndexDO.getDimensionIndexId());
            Integer fraction = param.get(i).getInteger("fraction");
            count = count.add(
                    (new BigDecimal(fraction *
                            examineDimensionIndexDO.getIndexWeight() *
                            examineDimensionDO.getDimensionWeight())
                    ));
        }
        List<ExamineResultConfigDO> examineResultConfigDOS = examineResultConfigMapper.selectList(new LambdaQueryWrapper<ExamineResultConfigDO>() {{
            eq(ExamineResultConfigDO::getExamineId, examineId);
        }});
        count = count.divide(new BigDecimal(10000)).setScale(0, BigDecimal.ROUND_HALF_UP);
        for (int i1 = 0; i1 < examineResultConfigDOS.size(); i1++) {
            Integer fractionStart = examineResultConfigDOS.get(i1).getFractionStart();
            Integer fractionEnd = examineResultConfigDOS.get(i1).getFractionEnd();
            if (count.compareTo(new BigDecimal(fractionStart)) >= 0 && count.compareTo(new BigDecimal(fractionEnd)) <= 0) {
                result.put("fraction", count.intValue());
                result.put("gradeName", examineResultConfigDOS.get(i1).getGradeName());
                return result;
            }
        }
        return null;
    }


    @Override
    @DataPermission(enable = false)
    public JSONObject selectExamineInfoByEmployee(JSONObject param) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        JSONObject result = new JSONObject();
        //1 已评分 2结果审核 3结果确认
        Integer type = param.getInteger("type");
        Integer examineId = param.getInteger("examineId");
        Integer recordEmployeeId = param.getInteger("recordEmployeeId");
        Integer scorerOrder = param.getInteger("scorerOrder");
        ExamineDO examineDO = examineMapper.selectById(examineId);
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectOne(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(ExamineRecordDO::getEmployeeId, recordEmployeeId);
        }});
        EmployeeDO examineEmployeeDO = employeeMapper.selectByUserId(recordEmployeeId.longValue());
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
            le(ExamineRecordRaterDO::getScorerOrder, scorerOrder);

        }});
        List<Integer> recordRaterIdList = examineRecordRaterDOS.stream().map(ExamineRecordRaterDO::getId).collect(Collectors.toList());
        List<ExamineRecordRaterIndexDO> examineRecordRaterIndexDOS = examineRecordRaterIndexMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
            in(ExamineRecordRaterIndexDO::getRaterId, recordRaterIdList);
            orderByAsc(ExamineRecordRaterIndexDO::getDimensionIndexId);
        }});
        List<JSONObject> resultList = new ArrayList<>();
        examineRecordRaterIndexDOS.forEach(examineRecordRaterIndexDO -> {
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineRecordRaterIndexDO.getDimensionId());
            ExamineDimensionIndexDO examineDimensionIndexDO = examineDimensionIndexMapper.selectById(examineRecordRaterIndexDO.getDimensionIndexId());
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectById(examineRecordRaterIndexDO.getRaterId());
//            DataPermissionUtils.executeIgnore(()->employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue()));
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject resultData = new JSONObject();
            resultData.put("id", examineRecordRaterIndexDO.getId());
            resultData.put("dimensionName", examineDimensionDO.getDimensionName());
            resultData.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            resultData.put("dimensionIndexName", examineDimensionIndexDO.getIndexName());
            resultData.put("dimensionIllustrateName", examineDimensionIndexDO.getIndexIllustrate());
            resultData.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
            resultData.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
            resultData.put("employeeName", employeeDO.getName());
            resultData.put("fraction", examineRecordRaterIndexDO.getFraction());
            resultData.put("rateComment", examineRecordRaterIndexDO.getRateComment());
            resultList.add(resultData);
        });
        if (examineRecordDO.getExanineType() == 1) {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                result.put("departmentName", dept.getCheckedData().getName());

            } else {
                result.put("departmentName", "");

            }
        } else {
            if (!ObjectUtils.isEmpty(examineRecordDO.getPostId())) {
                CommonResult<PostRespDTO> post = postApi.getPost(examineRecordDO.getPostId().longValue());
                if (!ObjectUtils.isEmpty(post.getData())) {
                    result.put("postName", post.getCheckedData().getName());
                } else {
                    result.put("postName", "");
                }
            } else {
                result.put("postName", "");
            }

        }
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);


        });
        ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
        }});
        result.put("cycleType", examineDO.getExamineCycleTypeId());
        result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        result.put("startTime", examineDO.getExamineCycleStart());
        result.put("endTime", examineDO.getExamineCycleEnd());
        result.put("year", examineDO.getExamineYear());
        result.put("state", examineRecordDO.getExamineStatus());
        result.put("commentResultList", commentResultList);
        result.put("list", resultList);
        result.put("name", examineDO.getExaminePlanName());
        result.put("fraction", examineRecordRaterDO.getRaterFraction());
        result.put("employeeName", examineEmployeeDO.getName());
        result.put("employmentForm", EmploymentFormEnum.getName(examineEmployeeDO.getEmploymentForm()));
        if (examineRecordRaterDO.getRaterGradeId() == -1) {
            result.put("gradeName", "");
        } else {
            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
            result.put("gradeName", examineResultConfigDO.getGradeName());
        }
        return result;
    }


    @Override
    @DataPermission(enable = false)
    public JSONObject selectExamineInfoById(JSONObject param) {
        JSONObject result = new JSONObject();
        Integer examineId = param.getInteger("examineId");
        Integer recordEmployeeId = param.getInteger("recordEmployeeId");
        ExamineDO examineDO = examineMapper.selectById(examineId);
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectOne(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(ExamineRecordDO::getEmployeeId, recordEmployeeId);
        }});
        EmployeeDO examineEmployeeDO = employeeMapper.selectByUserId(recordEmployeeId.longValue());
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
        }});
        List<Integer> recordRaterIdList = examineRecordRaterDOS.stream().map(ExamineRecordRaterDO::getId).collect(Collectors.toList());
        List<ExamineRecordRaterIndexDO> examineRecordRaterIndexDOS = examineRecordRaterIndexMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
            in(ExamineRecordRaterIndexDO::getRaterId, recordRaterIdList);
            orderByAsc(ExamineRecordRaterIndexDO::getDimensionIndexId);
        }});
        List<JSONObject> resultList = new ArrayList<>();
        examineRecordRaterIndexDOS.forEach(examineRecordRaterIndexDO -> {
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineRecordRaterIndexDO.getDimensionId());
            ExamineDimensionIndexDO examineDimensionIndexDO = examineDimensionIndexMapper.selectById(examineRecordRaterIndexDO.getDimensionIndexId());
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectById(examineRecordRaterIndexDO.getRaterId());
//            DataPermissionUtils.executeIgnore(()->employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue()));
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject resultData = new JSONObject();
            resultData.put("id", examineRecordRaterIndexDO.getId());
            resultData.put("dimensionName", examineDimensionDO.getDimensionName());
            resultData.put("dimensionIndexName", examineDimensionIndexDO.getIndexName());
            resultData.put("dimensionIllustrateName", examineDimensionIndexDO.getIndexIllustrate());
            resultData.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
            resultData.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
            resultData.put("employeeName", employeeDO.getName());
            resultData.put("fraction", examineRecordRaterIndexDO.getFraction());
            resultData.put("rateComment", examineRecordRaterIndexDO.getRateComment());
            resultData.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            resultList.add(resultData);
        });
        if (examineRecordDO.getExanineType() == 1) {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                result.put("departmentName", dept.getCheckedData().getName());

            } else {
                result.put("departmentName", "");

            }
        } else {
            if (!ObjectUtils.isEmpty(examineRecordDO.getPostId())) {
                CommonResult<PostRespDTO> post = postApi.getPost(examineRecordDO.getPostId().longValue());
                if (!ObjectUtils.isEmpty(post.getData())) {
                    result.put("postName", post.getCheckedData().getName());
                } else {
                    result.put("postName", "");
                }
            } else {
                result.put("postName", "");
            }
        }
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);
        });
        result.put("cycleType", examineDO.getExamineCycleTypeId());
        result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        result.put("startTime", examineDO.getExamineCycleStart());
        result.put("endTime", examineDO.getExamineCycleEnd());
        result.put("year", examineDO.getExamineYear());
        result.put("state", examineRecordDO.getExamineStatus());
        result.put("commentResultList", commentResultList);
        result.put("list", resultList);
        result.put("name", examineDO.getExaminePlanName());
        result.put("fraction", examineRecordDO.getRatingScore());
        result.put("recordId", examineRecordDO.getId());
        result.put("employeeName", examineEmployeeDO.getName());
        result.put("employmentForm", EmploymentFormEnum.getName(examineEmployeeDO.getEmploymentForm()));
        if (examineRecordDO.getExamineGradeId() == -1) {
            result.put("gradeName", "");
        } else {
            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
            result.put("gradeName", examineResultConfigDO.getGradeName());
        }
        return result;
    }

    @Override
    @DataPermission(enable = false)
    public JSONObject selectLaunchResultExamineInfoById(Integer examineId) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        JSONObject result = new JSONObject();
        ExamineDO examineDO = examineMapper.selectById(examineId);
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectOne(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(ExamineRecordDO::getEmployeeId, loginId);
        }});
        if (ObjectUtils.isEmpty(examineRecordDO)) {
            return new JSONObject();
        }
        EmployeeDO examineEmployeeDO = employeeMapper.selectByUserId(loginId);
        List<ExamineDimensionDO> examineDimensionDOS = examineDimensionMapper.selectList(new LambdaQueryWrapper<ExamineDimensionDO>() {{
            eq(ExamineDimensionDO::getExamineId, examineId);
        }});
        List<JSONObject> dimensionResultList = new ArrayList<>();
        examineDimensionDOS.forEach(examineDimensionDO -> {
            JSONObject dimensionResult = new JSONObject();

            dimensionResult.put("dimensionName", examineDimensionDO.getDimensionName());
            dimensionResult.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                eq(ExamineDimensionIndexDO::getIndexDimensionId, examineDimensionDO.getId());
            }});
            List<JSONObject> resultDataList = new ArrayList<>();
            examineDimensionIndexDOS.forEach(examineDimensionIndexDO -> {

                List<ExamineRecordRaterIndexDO> examineRecordRaterIndexDOS = examineRecordRaterIndexMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
                    eq(ExamineRecordRaterIndexDO::getDimensionIndexId, examineDimensionIndexDO.getId());
                    orderByAsc(ExamineRecordRaterIndexDO::getDimensionIndexId);
                }});
                JSONObject resultData = new JSONObject();
                resultData.put("dimensionIndexName", examineDimensionIndexDO.getIndexName());
                resultData.put("dimensionIllustrateName", examineDimensionIndexDO.getIndexIllustrate());
                resultData.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
                resultData.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
                List<JSONObject> indexResultList = new ArrayList<>();
                examineRecordRaterIndexDOS.forEach(examineRecordRaterIndexDO -> {
                    JSONObject indexResult = new JSONObject();
                    ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectById(examineRecordRaterIndexDO.getRaterId());
                    EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
                    indexResult.put("id", examineRecordRaterIndexDO.getId());
                    indexResult.put("employeeName", employeeDO.getName());
                    indexResult.put("fraction", examineRecordRaterIndexDO.getFraction());
                    indexResult.put("rateComment", examineRecordRaterIndexDO.getRateComment());
                    indexResultList.add(indexResult);
                });
                resultData.put("ratingList", indexResultList);
                resultDataList.add(resultData);
            });
            dimensionResult.put("indexList", resultDataList);
            dimensionResultList.add(dimensionResult);
        });
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
        }});
        if (examineRecordDO.getExanineType() == 1) {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                result.put("departmentName", dept.getCheckedData().getName());

            } else {
                result.put("departmentName", "");

            }
        } else {
            if (!ObjectUtils.isEmpty(examineRecordDO.getPostId())) {
                CommonResult<PostRespDTO> post = postApi.getPost(examineRecordDO.getPostId().longValue());
                if (!ObjectUtils.isEmpty(post.getData())) {
                    result.put("postName", post.getCheckedData().getName());
                } else {
                    result.put("postName", "");
                }
            } else {
                result.put("postName", "");
            }
        }
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);
        });
//        if (examineRecordDO.getCycleType() == 3 || examineRecordDO.getCycleType() == 4 || examineRecordDO.getCycleType() == 5) {
////            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
//            result.put("cycleTimeType",examineRecordDO.getExamineYear());
//        }else if(examineRecordDO.getCycleType() == 6) {
//            result.put("cycleTimeType",examineRecordDO.getCycleStartTime()+"-"+examineRecordDO.getCycleEndTime());
//        }else {
//            result.put("cycleTimeType",examineRecordDO.getCycleTimeType());
//        }
        result.put("cycleType", examineDO.getExamineCycleTypeId());
        result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        result.put("startTime", examineDO.getExamineCycleStart());
        result.put("endTime", examineDO.getExamineCycleEnd());
        result.put("year", examineDO.getExamineYear());
        result.put("state", examineRecordDO.getExamineStatus());
        result.put("commentResultList", commentResultList);
        result.put("list", dimensionResultList);
        result.put("name", examineDO.getExaminePlanName());
        result.put("fraction", examineRecordDO.getFraction());
        result.put("recordId", examineRecordDO.getId());
        result.put("employeeName", examineEmployeeDO.getName());
        result.put("employmentForm", EmploymentFormEnum.getName(examineEmployeeDO.getEmploymentForm()));
        if (examineRecordDO.getExamineGradeId() == -1) {
            result.put("gradeName", "");
        } else {
            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
            result.put("gradeName", examineResultConfigDO.getGradeName());
        }
        return result;


    }

    @Override
    public List<JSONObject> selectLaunchResultList() {
        List<JSONObject> resultList = new ArrayList<>();
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getEmployeeId, loginId);
            in(ExamineRecordDO::getExamineStatus, 5, 6);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            JSONObject result = new JSONObject();
            if (examineRecordDO.getCycleType() == 3 || examineRecordDO.getCycleType() == 4 || examineRecordDO.getCycleType() == 5) {
                examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年");
            } else if (examineRecordDO.getCycleType() == 6) {
                examineRecordDO.setCycleTimeType(examineRecordDO.getCycleStartTime() + "-" + examineRecordDO.getCycleEndTime());
            } else if (examineRecordDO.getCycleType() == 2) {
                if (examineRecordDO.getCycleTimeType().equals("1")) {
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年" + "第一季度");
                } else if (examineRecordDO.getCycleTimeType().equals("2")) {
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年" + "第二季度");
                } else if (examineRecordDO.getCycleTimeType().equals("3")) {
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年" + "第三季度");
                } else if (examineRecordDO.getCycleTimeType().equals("4")) {
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年" + "第四季度");
                }

                examineRecordDO.setCycleTimeType(examineRecordDO.getCycleTimeType());
            } else {
                examineRecordDO.setCycleTimeType(examineRecordDO.getCycleTimeType());
            }
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            String s = DateUtil.stringDateToMouth(examineDO.getCreateTime());
            examineRecordDO.setTitleTime(s);
            result.put("img", "");
            result.put("examineCreateTimeStr", s.replace("-", "年") + "月");
            result.put("examineCreateTime", s);
            result.put("examineState", examineRecordDO.getExamineStatus());
            result.put("examineId", examineDO.getId());
            resultList.add(result);
        });
        return resultList;
    }


    @Override
    public List<ExamineRecordDO> selectStartReviewList(Integer examineId, Integer type) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            in(ExamineRecordDO::getExamineStatus, 2);
            orderByDesc(ExamineRecordDO::getId);
        }});
        if (type == 1) {
            int size = examineRecordDOS.stream().filter(examineRecordDO -> examineRecordDO.getExamineStatus() == 1).collect(Collectors.toList()).size();
            if (size > 0) {
                throw exception(RESULT_SCORE_NOT_EXISTS);
            }
        }
        examineRecordDOS.forEach(examineRecordDO -> {
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());

            }
            examineRecordDO.setExamineId(employeeDO.getId().intValue());
            examineRecordDO.setFraction(examineRecordDO.getRatingScore());
            if (examineRecordDO.getExamineGradeId() == -1) {
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }


        });
        return examineRecordDOS;
    }


    @Override
    public Integer startReview(Integer examineId) {
//        Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
////            eq(ExamineRecordDO::getExamineStatus, 1);
//            eq(ExamineRecordDO::getExamineId, examineId);
//        }});
//        if (l > 0) {
//            throw exception(RESULT_SCORE_NOT_EXISTS);
//        }
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(ExamineRecordDO::getExamineStatus, 2);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            ExamineResultDO examineResultDO = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getRecordId, examineRecordDO.getId());
                in(ExamineResultDO::getResultStatus, 0, 3);
                orderByAsc(ExamineResultDO::getExamineResultGrade);
                last("limit 1");
            }});
            if (ObjectUtils.isEmpty(examineResultDO)) {

            } else {
                examineResultDO.setResultStatus(1);
                examineResultMapper.updateById(examineResultDO);
                List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                    eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                }});
                examineRecordDO.setExamineStatus(3);
                examineRecordMapper.updateById(examineRecordDO);
                examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
                    examineRecordRaterDO.setRaterStatus(2);
                    examineRecordRaterMapper.updateById(examineRecordRaterDO);
                });
                NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
                Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
                templateParams.put("info", "您有新的绩效结果待审核，请及时查看");
                singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineResultDO.getResultEmployeeId()));
                singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.REVIEW.getCode());
                singleToUserReqDTO.setTemplateParams(templateParams);
                singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_REVIEW.getCode());
                JSONObject options = new JSONObject();
                options.put("examineId", examineRecordDO.getExamineId());
                options.put("recordEmployeeId", examineRecordDO.getEmployeeId());
                options.put("type", 2);
                options.put("recordId", examineResultDO.getRecordId());
                singleToUserReqDTO.setType(1);
                singleToUserReqDTO.setOptions(options.toJSONString());

                messageSendApi.sendSingleNotify(singleToUserReqDTO);
            }
        });
        return 1;
    }

    @Override
    public List<ExamineRecordDO> selectExamineScheduleList(Integer examineId, Integer gradeId, String employeeName, Integer type) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(gradeId != 0, ExamineRecordDO::getExamineGradeId, gradeId);
            like(!ObjectUtils.isEmpty(employeeName), ExamineRecordDO::getRecordEmployeeName, employeeName);
        }});
        if (type == 1) {
            int size = examineRecordDOS.stream().filter(examineRecordDO -> examineRecordDO.getExamineStatus() == 1).collect(Collectors.toList()).size();
            if (size > 0) {
                throw exception(RESULT_SCORE_NOT_EXISTS);
            }
        }
        examineRecordDOS.forEach(examineRecordDO -> {
            if (examineRecordDO.getExamineGradeId() == -1) {
                examineRecordDO.setGradeName("");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
            examineRecordDO.setFraction(examineRecordDO.getRatingScore());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());
            }
            examineRecordDO.setExamineId(employeeDO.getId().intValue());
            if (examineRecordDO.getExamineStatus() == 1 || examineRecordDO.getExamineStatus() == 2 || examineRecordDO.getExamineStatus() == 6) {
                ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                    eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                    in(ExamineRecordRaterDO::getRaterStatus, 0);
                    orderByAsc(ExamineRecordRaterDO::getScorerOrder);
                }});
                if (ObjectUtils.isEmpty(examineRecordRaterDO)) {
                    ExamineRecordRaterDO examineRecordRater = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                        eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                        orderByAsc(ExamineRecordRaterDO::getScorerOrder);
                        last("limit 1");
                    }});
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordRater.getRaterEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                } else {
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                }

            }
            if (examineRecordDO.getExamineStatus() == 3) {
                ExamineResultDO examineResultDO = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                    eq(ExamineResultDO::getRecordId, examineRecordDO.getId());
                    eq(ExamineResultDO::getResultStatus, 1);
                    orderByAsc(ExamineResultDO::getExamineResultGrade);
                    last("limit 1");
                }});
                if (ObjectUtils.isEmpty(examineResultDO)) {
                    ExamineResultDO examineResult = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                        eq(ExamineResultDO::getRecordId, examineRecordDO.getId());
                        orderByAsc(ExamineResultDO::getExamineResultGrade);
                        last("limit 1");
                    }});
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineResult.getResultEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                } else {
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineResultDO.getResultEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                }
            }
            if (examineRecordDO.getExamineStatus() == 4) {
                EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
            }

        });
        return examineRecordDOS;
    }

    @Override
    @DataPermission(enable = false)
    public List<ExamineRecordDO> selectExamineScheduleEmployeeList(Integer examineId, Integer status, String employeeName) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            if (status == 0) {
                le(ExamineRecordDO::getExamineStatus, 0);
            }
            if (status == 1) {
                in(ExamineRecordDO::getExamineStatus, 2, 1, 8, 7);
            }
            if (status == 2) {
                in(ExamineRecordDO::getExamineStatus, 3, 4);
            }
            if (status == 3) {
                in(ExamineRecordDO::getExamineStatus, 5, 6);
            }
            if (status == 4) {
                in(ExamineRecordDO::getExamineStatus, 9, 10);
            }
            if (status == 6) {
                in(ExamineRecordDO::getIsLowPerformance, 1);
                groupBy(ExamineRecordDO::getEmployeeId);
            }
            like(!ObjectUtils.isEmpty(employeeName), ExamineRecordDO::getRecordEmployeeName, employeeName);
        }});
        //todo 全部数据会提示
//        int size = examineRecordDOS.stream().filter(examineRecordDO -> examineRecordDO.getExamineStatus() == 1).collect(Collectors.toList()).size();
//        if (size > 0) {
//            throw exception(RESULT_SCORE_NOT_EXISTS);
//        }
        examineRecordDOS.forEach(examineRecordDO -> {
            if (examineRecordDO.getExamineGradeId() == -1) {
                examineRecordDO.setGradeName("");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
            examineRecordDO.setFraction(examineRecordDO.getRatingScore());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());
            }
            examineRecordDO.setExamineId(employeeDO.getId().intValue());
            examineRecordDO.setEmployeeStatus(employeeDO.getState() + "");
            if (examineRecordDO.getExamineStatus() == 1 || examineRecordDO.getExamineStatus() == 2 || examineRecordDO.getExamineStatus() == 8) {
                ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                    eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                    in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
                    orderByAsc(ExamineRecordRaterDO::getScorerOrder);
                    last("limit 1");
                }});
                if (ObjectUtils.isEmpty(examineRecordRaterDO)) {
                    ExamineRecordRaterDO examineRecordRater = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                        eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
                        orderByDesc(ExamineRecordRaterDO::getScorerOrder);
                        last("limit 1");
                    }});
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordRater.getRaterEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                } else {
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                }

            }
            if (examineRecordDO.getExamineStatus() == 3 || examineRecordDO.getExamineStatus() == 4) {
                ExamineResultDO examineResultDO = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                    eq(ExamineResultDO::getRecordId, examineRecordDO.getId());
                    eq(ExamineResultDO::getResultStatus, 1);
                    orderByAsc(ExamineResultDO::getExamineResultGrade);
                    last("limit 1");
                }});
                if (ObjectUtils.isEmpty(examineResultDO)) {
                    ExamineResultDO examineResult = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                        eq(ExamineResultDO::getRecordId, examineRecordDO.getId());
                        orderByAsc(ExamineResultDO::getExamineResultGrade);
                        last("limit 1");
                    }});
                    if (!ObjectUtils.isEmpty(examineResult)) {
                        EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineResult.getResultEmployeeId().longValue());
                        examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                    }

                } else {
                    EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineResultDO.getResultEmployeeId().longValue());
                    examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
                }
            }
            if (examineRecordDO.getExamineStatus() == 5 || examineRecordDO.getExamineStatus() == 6) {
                EmployeeDO flowPathEmployeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                examineRecordDO.setPendingPerson(flowPathEmployeeDO.getName());
            }

        });
        return examineRecordDOS;
    }

    @Override
    public Integer review(JSONObject param) {
        Integer recordId = param.getInteger("recordId");
        Integer nodeId = param.getInteger("nodeId");
        Integer status = param.getInteger("status");
        String reason = param.getString("reason");
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        ExamineResultDO examineResultDO = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
            eq(ExamineResultDO::getRecordId, recordId);
            eq(ExamineResultDO::getResultStatus, 1);
            eq(ExamineResultDO::getResultEmployeeId, loginId);
        }});
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
        if (status == 1) {
            examineResultDO.setResultStatus(2);
            int i = examineResultMapper.updateById(examineResultDO);
            ;
            ExamineResultDO examineResult = examineResultMapper.selectOne(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getRecordId, recordId);
                eq(ExamineResultDO::getResultStatus, 0);
                orderByAsc(ExamineResultDO::getExamineResultGrade);
                last("limit 1");
            }});
            if (ObjectUtils.isEmpty(examineResult)) {
                Long l = examineResultMapper.selectCount(new LambdaQueryWrapper<ExamineResultDO>() {{
                    eq(ExamineResultDO::getRecordId, recordId);
                    in(ExamineResultDO::getResultStatus, 0, 1, 3);
                    orderByAsc(ExamineResultDO::getExamineResultGrade);
                    last("limit 1");
                }});
                if (l <= 0) {
                    examineRecordDO.setExamineStatus(4);
                    examineRecordMapper.updateById(examineRecordDO);
                    List<ExamineRecordRaterDO> examineRecordRaterDOs = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                        eq(ExamineRecordRaterDO::getRecordId, recordId);
                    }});
                    examineRecordRaterDOs.forEach(examineRecordRaterDO -> {
                        examineRecordRaterDO.setRaterStatus(3);
                        examineRecordRaterMapper.updateById(examineRecordRaterDO);
                    });


                }

            } else {
                examineResult.setResultStatus(1);
                examineResultMapper.updateById(examineResult);
                NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
                Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
                templateParams.put("info", "您有新的绩效结果待审核，请及时查看");
                singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineResult.getResultEmployeeId()));
                singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.REVIEW.getCode());
                singleToUserReqDTO.setTemplateParams(templateParams);
                singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_REVIEW.getCode());
                JSONObject options = new JSONObject();
                options.put("examineId", examineResult.getExamineId());
                options.put("recordEmployeeId", examineRecordDO.getEmployeeId());
                options.put("type", 2);
                options.put("recordId", examineResult.getRecordId());
                singleToUserReqDTO.setType(1);
                singleToUserReqDTO.setOptions(options.toJSONString());
                messageSendApi.sendSingleNotify(singleToUserReqDTO);
            }
        } else {

            examineRecordDO.setExamineStatus(8);
            examineRecordMapper.updateById(examineRecordDO);
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getRecordId, recordId);
                ne(ExamineResultDO::getId, examineResultDO.getId());
            }});
            examineResultDOS.forEach(examineResultDO1 -> {
                examineResultDO1.setResultStatus(0);
                examineResultMapper.updateById(examineResultDO1);
            });
            examineResultDO.setResultStatus(0);
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, nodeId);
            }});
            examineRecordRaterDO.setRaterStatus(6);
            examineRecordRaterDO.setRejectReason(reason);
            List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                gt(ExamineRecordRaterDO::getScorerOrder, examineRecordRaterDO.getScorerOrder());
            }});
            examineRecordRaterDOS.forEach(examineRecordRaterDO1 -> {
                examineRecordRaterDO1.setRaterStatus(-1);
                examineRecordRaterMapper.updateById(examineRecordRaterDO1);
            });
            int i = examineRecordRaterMapper.updateById(examineRecordRaterDO);

            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效结果已驳回，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordRaterDO.getRaterEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.SCORE.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
            JSONObject options = new JSONObject();
            options.put("id", examineRecordRaterDO.getRecordId());
            options.put("scorerOrder", examineRecordRaterDO.getScorerOrder());
            options.put("type", 1);
            options.put("recordId", examineRecordRaterDO.getRecordId());
            singleToUserReqDTO.setType(1);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        }
        int i = examineResultMapper.updateById(examineResultDO);
        return i;
    }

    @Override
    @DataPermission(enable = false)
    public List<JSONObject> nodeList(Integer recordId) {
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getRecordId, recordId);
        }});
        List<JSONObject> resultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject result = new JSONObject();
            result.put("id", examineRecordRaterDO.getRaterEmployeeId());
            result.put("value", employeeDO.getName());
            resultList.add(result);
        });
        return resultList;
    }

    @Override
    @DataPermission(enable = false)
    public Integer confirm(JSONObject param) {
        Integer recordId = param.getInteger("recordId");
        Integer nodeId = param.getInteger("nodeId");
        Integer status = param.getInteger("status");
        String reason = param.getString("reason");
//        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
        if (status == 1) {
            examineRecordDO.setExamineStatus(6);
            int i = examineRecordMapper.updateById(examineRecordDO);
            Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                in(ExamineRecordDO::getExamineStatus, 1, 2, 3, 4, 5, 7, 8);
                eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
            }});
            if (l <= 0) {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                examineDO.setExamineState(8);
                examineMapper.updateById(examineDO);
                List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
                    eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
                }});
                examineRecordDOS.forEach(record -> {
                    record.setExamineStatus(9);
                    examineRecordMapper.updateById(record);
                });
                NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
                Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
                templateParams.put("info", "您提交的绩效确认已完成，请及时查看");
                singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineDO.getCreateBy()));
                singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.RATING_CONFIRMATION.getCode());
                singleToUserReqDTO.setTemplateParams(templateParams);
                singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_CONFIRM.getCode());
                JSONObject options = new JSONObject();
                options.put("examineId", examineRecordDO.getExamineId());
                options.put("status", -1);
                options.put("type", 2);
                singleToUserReqDTO.setType(2);
                singleToUserReqDTO.setOptions(options.toJSONString());
                messageSendApi.sendSingleNotify(singleToUserReqDTO);
            }
        } else {
            examineRecordDO.setExamineStatus(8);
            examineRecordDO.setSendStatus(2);
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, nodeId);
            }});
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getRecordId, recordId);
            }});
            examineResultDOS.forEach(examineResultDO1 -> {
                examineResultDO1.setResultStatus(0);
                examineResultMapper.updateById(examineResultDO1);
            });
            examineRecordRaterDO.setRaterStatus(6);
            examineRecordRaterDO.setAppealReason(reason);
            examineRecordRaterMapper.updateById(examineRecordRaterDO);
            List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                gt(ExamineRecordRaterDO::getScorerOrder, examineRecordRaterDO.getScorerOrder());
            }});
            examineRecordRaterDOS.forEach(examineRecordRaterDO1 -> {
                examineRecordRaterDO1.setRaterStatus(-1);
                examineRecordRaterMapper.updateById(examineRecordRaterDO1);
            });
            examineRecordMapper.updateById(examineRecordDO);
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效申诉，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordRaterDO.getRaterEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.SCORE.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
            JSONObject options = new JSONObject();
            options.put("id", examineRecordRaterDO.getRecordId());
            options.put("scorerOrder", examineRecordRaterDO.getScorerOrder());
            options.put("type", 1);
            options.put("recordId", examineRecordRaterDO.getRecordId());
            singleToUserReqDTO.setType(1);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        }
        return 1;
    }


    @Override
    @DataPermission(enable = false)
    public Integer adminConfirm(JSONObject param) {
        Integer recordId = param.getInteger("recordId");
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
        examineRecordDO.setExamineStatus(6);
        int i = examineRecordMapper.updateById(examineRecordDO);
        Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
            in(ExamineRecordDO::getExamineStatus, 1, 2, 3, 4, 5, 7, 8);
            eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
        }});
        if (l <= 0) {
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            examineDO.setExamineState(8);
            examineMapper.updateById(examineDO);
            List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
            }});
            examineRecordDOS.forEach(record -> {
                record.setExamineStatus(9);
                examineRecordMapper.updateById(record);
            });
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您提交的绩效确认已完成，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineDO.getCreateBy()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.RATING_CONFIRMATION.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_CONFIRM.getCode());
            JSONObject options = new JSONObject();
            options.put("examineId", examineRecordDO.getExamineId());
            options.put("status", -1);
            options.put("type", 2);
            singleToUserReqDTO.setType(2);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        }

        return 1;
    }

    @Override
    public Integer confirmByAdmin(JSONObject param) {
        Integer recordId = param.getInteger("recordId");
        Integer nodeId = param.getInteger("nodeId");
        Integer status = param.getInteger("status");
        String reason = param.getString("reason");
//        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
        if (status == 1) {
            examineRecordDO.setExamineStatus(6);
            int i = examineRecordMapper.updateById(examineRecordDO);
            Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                in(ExamineRecordDO::getExamineStatus, 1, 2, 3, 4, 5, 7, 8);
                eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
            }});
            if (l <= 0) {
                ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
                examineDO.setExamineState(8);
                examineMapper.updateById(examineDO);
                List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
                    eq(ExamineRecordDO::getExamineId, examineRecordDO.getExamineId());
                }});
                examineRecordDOS.forEach(record -> {
                    record.setExamineStatus(9);
                    examineRecordMapper.updateById(record);
                });
                NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
                Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
                templateParams.put("info", "您提交的绩效确认已完成，请及时查看");
                singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineDO.getCreateBy()));
                singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.RATING_CONFIRMATION.getCode());
                singleToUserReqDTO.setTemplateParams(templateParams);
                singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_CONFIRM.getCode());
                JSONObject options = new JSONObject();
                options.put("examineId", examineRecordDO.getExamineId());
                options.put("status", -1);
                options.put("type", 2);
                singleToUserReqDTO.setType(2);
                singleToUserReqDTO.setOptions(options.toJSONString());
                messageSendApi.sendSingleNotify(singleToUserReqDTO);
            }
        } else {
            examineRecordDO.setExamineStatus(8);
            examineRecordDO.setSendStatus(2);
            ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectOne(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                eq(ExamineRecordRaterDO::getRaterEmployeeId, nodeId);
            }});
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getRecordId, recordId);
            }});
            examineResultDOS.forEach(examineResultDO1 -> {
                examineResultDO1.setResultStatus(0);
                examineResultMapper.updateById(examineResultDO1);
            });
            examineRecordRaterDO.setRaterStatus(6);
            examineRecordRaterDO.setAppealReason(reason);
            examineRecordRaterMapper.updateById(examineRecordRaterDO);
            List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                gt(ExamineRecordRaterDO::getScorerOrder, examineRecordRaterDO.getScorerOrder());
            }});
            examineRecordRaterDOS.forEach(examineRecordRaterDO1 -> {
                examineRecordRaterDO1.setRaterStatus(-1);
                examineRecordRaterMapper.updateById(examineRecordRaterDO1);
            });
            examineRecordMapper.updateById(examineRecordDO);
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效申诉，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordRaterDO.getRaterEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.SCORE.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
            JSONObject options = new JSONObject();
            options.put("id", examineRecordRaterDO.getRecordId());
            options.put("scorerOrder", examineRecordRaterDO.getScorerOrder());
            options.put("type", 1);
            options.put("recordId", examineRecordRaterDO.getRecordId());
            singleToUserReqDTO.setType(1);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        }
        return 1;
    }

    @Override
    @DataPermission(enable = false)
    public List<ExamineRecordDO> launchResultList(Integer examineId) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            in(ExamineRecordDO::getExamineStatus, 4, 5);
            orderByDesc(ExamineRecordDO::getId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());
            }
            examineRecordDO.setIsDimission(0);
            if (null != employeeDO.getApplySeparationTime()) {
                boolean before = employeeDO.getPlanSeparationTime().isBefore(LocalDateTime.now());
                examineRecordDO.setIsDimission(before ? 1 : 0);
            }

            if (examineRecordDO.getExamineGradeId() == -1) {
                examineRecordDO.setGradeName("");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
        });


        return examineRecordDOS;
    }

    @Override
    @DataPermission(enable = false)
    public Integer launchResult(JSONObject param) {
        List<Integer> recordIdLists = param.getList("recordIdList", Integer.class);
        Integer examineId = param.getInteger("examineId");
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            in(ExamineRecordDO::getEmployeeId, recordIdLists);
            eq(ExamineRecordDO::getExamineId, examineId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            examineRecordDO.setSendStatus(1);
            examineRecordDO.setExamineStatus(5);
            examineRecordMapper.updateById(examineRecordDO);
            ExamineRecordRaterDO examineRecordRaterDO = new ExamineRecordRaterDO();
            examineRecordRaterDO.setRaterStatus(4);
            examineRecordRaterMapper.update(examineRecordRaterDO, new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            }});
            NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
            Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
            templateParams.put("info", "您有新的绩效待确认，请及时查看");
            singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineRecordDO.getEmployeeId()));
            singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.CONFIRMATION.getCode());
            singleToUserReqDTO.setTemplateParams(templateParams);
            singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_REVIEW.getCode());
            JSONObject options = new JSONObject();
            options.put("actives", 2);
            options.put("examineId", examineRecordDO.getExamineId());
            options.put("recordId", examineRecordDO.getId());
            options.put("recordEmployeeId", examineRecordDO.getEmployeeId());
            options.put("type", 3);
            singleToUserReqDTO.setType(1);
            singleToUserReqDTO.setOptions(options.toJSONString());
            messageSendApi.sendSingleNotify(singleToUserReqDTO);
        });
        ExamineDO examineDO = examineMapper.selectById(examineId);
        examineDO.setExamineState(3);
        examineMapper.updateById(examineDO);
        return 1;
    }

    @Override
    @DataPermission(enable = false)
    public Integer withdrawResult(JSONObject param) {
        List<Integer> recordIdLists = param.getList("recordIdList", Integer.class);
        Integer examineId = param.getInteger("examineId");
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            in(ExamineRecordDO::getEmployeeId, recordIdLists);
            eq(ExamineRecordDO::getExamineId, examineId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            examineRecordDO.setSendStatus(2);
            examineRecordDO.setExamineStatus(4);
            examineRecordMapper.updateById(examineRecordDO);
            ExamineRecordRaterDO examineRecordRaterDO = new ExamineRecordRaterDO();
            examineRecordRaterDO.setRaterStatus(3);
            examineRecordRaterMapper.update(examineRecordRaterDO, new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            }});
        });

        ExamineDO examineDO = examineMapper.selectById(examineId);
        examineDO.setExamineState(2);
        examineMapper.updateById(examineDO);
        return 1;

    }

    @Override
    @DataPermission(enable = false)
    public JSONObject selectLaunchResultExamineInfoByIdWeb(Integer examineId) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        JSONObject result = new JSONObject();
        ExamineDO examineDO = examineMapper.selectById(examineId);
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectOne(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            eq(ExamineRecordDO::getEmployeeId, loginId);
        }});
        if (ObjectUtils.isEmpty(examineRecordDO)) {
            return new JSONObject();
        }
        EmployeeDO examineEmployeeDO = employeeMapper.selectByUserId(loginId);
        List<ExamineDimensionDO> examineDimensionDOS = examineDimensionMapper.selectList(new LambdaQueryWrapper<ExamineDimensionDO>() {{
            eq(ExamineDimensionDO::getExamineId, examineId);
        }});
        List<JSONObject> dimensionResultList = new ArrayList<>();
        examineDimensionDOS.forEach(examineDimensionDO -> {
            JSONObject dimensionResult = new JSONObject();

            dimensionResult.put("dimensionName", examineDimensionDO.getDimensionName());
            dimensionResult.put("dimensionWeight", examineDimensionDO.getDimensionWeight());
            List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
                eq(ExamineDimensionIndexDO::getIndexDimensionId, examineDimensionDO.getId());
            }});
            List<JSONObject> resultDataList = new ArrayList<>();
            examineDimensionIndexDOS.forEach(examineDimensionIndexDO -> {

                List<ExamineRecordRaterIndexDO> examineRecordRaterIndexDOS = examineRecordRaterIndexMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterIndexDO>() {{
                    eq(ExamineRecordRaterIndexDO::getDimensionIndexId, examineDimensionIndexDO.getId());
                    orderByAsc(ExamineRecordRaterIndexDO::getDimensionIndexId);
                }});
                JSONObject resultData = new JSONObject();
                resultData.put("dimensionIndexName", examineDimensionIndexDO.getIndexName());
                resultData.put("dimensionIllustrateName", examineDimensionIndexDO.getIndexIllustrate());
                resultData.put("indexStandard", examineDimensionIndexDO.getIndexStandard());
                resultData.put("indexWeight", examineDimensionIndexDO.getIndexWeight());
                List<JSONObject> indexResultList = new ArrayList<>();
                examineRecordRaterIndexDOS.forEach(examineRecordRaterIndexDO -> {
                    JSONObject indexResult = new JSONObject();
                    ExamineRecordRaterDO examineRecordRaterDO = examineRecordRaterMapper.selectById(examineRecordRaterIndexDO.getRaterId());
                    EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
                    indexResult.put("id", examineRecordRaterIndexDO.getId());
                    indexResult.put("employeeName", employeeDO.getName());
                    indexResult.put("fraction", examineRecordRaterIndexDO.getFraction());
                    indexResult.put("rateComment", examineRecordRaterIndexDO.getRateComment());
                    indexResultList.add(indexResult);
                });
                resultData.put("ratingList", indexResultList);
                resultDataList.add(resultData);
            });
            dimensionResult.put("indexList", resultDataList);
            dimensionResultList.add(dimensionResult);
        });
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
        }});
        if (examineRecordDO.getExanineType() == 1) {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(examineRecordDO.getDepartmentId().longValue());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                result.put("departmentName", dept.getCheckedData().getName());

            }
        } else {
            if (!ObjectUtils.isEmpty(examineRecordDO.getPostId())) {
                CommonResult<PostRespDTO> post = postApi.getPost(examineRecordDO.getPostId().longValue());
                if (!ObjectUtils.isEmpty(post.getData())) {
                    result.put("postName", post.getCheckedData().getName());
                } else {
                    result.put("postName", "");
                }
            } else {
                result.put("postName", "");
            }
        }
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);
        });
//        if (examineRecordDO.getCycleType() == 3 || examineRecordDO.getCycleType() == 4 || examineRecordDO.getCycleType() == 5) {
////            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
//            result.put("cycleTimeType",examineRecordDO.getExamineYear());
//        }else if(examineRecordDO.getCycleType() == 6) {
//            result.put("cycleTimeType",examineRecordDO.getCycleStartTime()+"-"+examineRecordDO.getCycleEndTime());
//        }else {
//            result.put("cycleTimeType",examineRecordDO.getCycleTimeType());
//        }
        result.put("cycleType", examineDO.getExamineCycleTypeId());
        result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        result.put("startTime", examineDO.getExamineCycleStart());
        result.put("endTime", examineDO.getExamineCycleEnd());
        result.put("year", examineDO.getExamineYear());
        result.put("state", examineRecordDO.getExamineStatus());
        result.put("commentResultList", commentResultList);
        result.put("list", dimensionResultList);
        result.put("name", examineDO.getExaminePlanName());
        result.put("fraction", examineRecordDO.getFraction());
        result.put("recordId", examineRecordDO.getId());
        result.put("employeeName", examineEmployeeDO.getName());
        result.put("employmentForm", EmploymentFormEnum.getName(examineEmployeeDO.getEmploymentForm()));
        if (examineRecordDO.getExamineGradeId() == -1) {
            result.put("gradeName", "");
        } else {
            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
            result.put("gradeName", examineResultConfigDO.getGradeName());
        }
        return result;
    }

    @Override
    public Integer examineFile(Integer examineId) {
        ExamineDO examineDO = examineMapper.selectById(examineId);
        Long count = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
            notIn(ExamineRecordDO::getExamineStatus, 9, 10);
        }});
        if (count > 0) {
            throw exception(NOT_FLOW_PATH);
        }
        examineDO.setExamineState(9);
        examineMapper.updateById(examineDO);
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            examineRecordDO.setExamineStatus(10);
            List<ExamineFlowPerformanceDO> list = examineFlowPerformanceMapper.selectList(new LambdaQueryWrapper<ExamineFlowPerformanceDO>() {{
                eq(ExamineFlowPerformanceDO::getExamineId, examineId);
            }});
            if (list == null || list.size() <= 0) {
//                throw exception(NOT_LOW_PERFORMANCE);
                int i = examineRecordMapper.updateById(examineRecordDO);
            } else {
                list.forEach(l -> {
                    if (l.getConditionType() == 1) {
                        if (examineRecordDO.getExamineGradeId() == -1) {

                        } else {
                            ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                            List<String> collect = Stream.of(l.getConditionValue().split(",")).collect(Collectors.toList());
                            if (collect.contains(examineResultConfigDO.getId().toString())) {
                                examineRecordDO.setIsLowPerformance(1);
                                riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);

                            }
                        }
                    } else {
                        EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
                        switch (l.getUnit()) {
                            case 1:
                                if (examineRecordDO.getRatingScore() == Integer.parseInt(l.getConditionValue())) {
                                    examineRecordDO.setIsLowPerformance(1);
                                    riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);
                                }
                                break;
                            case 2:
                                if (examineRecordDO.getRatingScore() < Integer.parseInt(l.getConditionValue())) {
                                    examineRecordDO.setIsLowPerformance(1);
                                    riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);
                                }
                                break;
                            case 3:
                                if (examineRecordDO.getRatingScore() > Integer.parseInt(l.getConditionValue())) {
                                    examineRecordDO.setIsLowPerformance(1);
                                    riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);
                                }
                                break;
                            case 4:
                                if (examineRecordDO.getRatingScore() <= Integer.parseInt(l.getConditionValue())) {
                                    examineRecordDO.setIsLowPerformance(1);
                                    riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);
                                }
                                break;
                            case 5:
                                if (examineRecordDO.getRatingScore() >= Integer.parseInt(l.getConditionValue())) {
                                    examineRecordDO.setIsLowPerformance(1);
                                    riskService.addRiskToEmployee(employeeDO.getId(), 2000L, 1);
                                }
                                break;
                        }
                    }
                });
                if (examineRecordDO.getIsLowPerformance() == 1) {

                }
                int i = examineRecordMapper.updateById(examineRecordDO);
            }
        });
        NotifySendSingleToUserReqDTO singleToUserReqDTO = new NotifySendSingleToUserReqDTO();
        Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
        templateParams.put("info", "归档完成低绩效，请及时查看");
        singleToUserReqDTO.setUserId(Integer.toUnsignedLong(examineDO.getCreateBy()));
        singleToUserReqDTO.setDetailType(NotifyDetailTypeEnum.LOW_PERFORMANCE.getCode());
        singleToUserReqDTO.setTemplateParams(templateParams);
        singleToUserReqDTO.setTemplateCode(NotifyTemplateCodeEnum.EXAMINE_PROCESS.getCode());
        JSONObject options = new JSONObject();
        options.put("examineId", examineDO.getId());
        options.put("status", 6);
        singleToUserReqDTO.setType(2);
        singleToUserReqDTO.setOptions(options.toJSONString());
        messageSendApi.sendSingleNotify(singleToUserReqDTO);

        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getExamineId, examineId);
        }});
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            examineRecordRaterDO.setRaterStatus(7);
            examineRecordRaterMapper.updateById(examineRecordRaterDO);
        });
        return 1;
    }

    @Override
    @DataPermission(enable = false)
    public PageResult<ExamineRecordDO> selectExamineFileList(Integer pageNum, Integer pageSize,
                                                             String employeeName,
                                                             String employeeNo,
                                                             Integer deptId,
                                                             Integer postId,
                                                             Integer employeeState,
                                                             Integer sourceUnit,
                                                             Integer sources) {
        PageParam param = new PageParam();
        param.setPageSize(pageSize);
        param.setPageNo(pageNum);
        pageNum = pageNum - 1;
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectByRateStatus(pageNum, pageSize, employeeName, employeeNo, deptId, postId, employeeState, sourceUnit, sources);
        PageResult<ExamineRecordDO> pageResult = new PageResult<>();
        pageResult.setList(examineRecordDOS);
        pageResult.setTotal(examineRecordMapper.selectByRateStatusCount(pageNum, pageSize, employeeName, employeeNo, deptId, postId, employeeState, sourceUnit, sources));
        for (ExamineRecordDO examineRecordDO : pageResult.getList()) {
            Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getEmployeeId, examineRecordDO.getEmployeeId());
                eq(ExamineRecordDO::getExamineStatus, 10);
            }});
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            if (employeeDO == null) {
                pageResult.getList().remove(examineRecordDO);
                continue;
            }
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            String name = EmployeeStatusEnum.getName(employeeDO.getState());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineRecordDO.getExaminePlanName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());
            }
            if (!ObjectUtils.isEmpty(post.getData())) {
                examineRecordDO.setPostName(post.getCheckedData().getName());
            }
            examineRecordDO.setEmployeeStatus(name);
            examineRecordDO.setExamineNum(l.intValue());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
                examineRecordDO.setGradeName("");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
        }
        return pageResult;
    }


    public List<ExamineRecordDO> selectExamineFileExcel() {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineStatus, 10);
            groupBy(ExamineRecordDO::getEmployeeId);
        }});

        examineRecordDOS.forEach(examineRecordDO -> {
            Long l = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
                eq(ExamineRecordDO::getEmployeeId, examineRecordDO.getEmployeeId());
                eq(ExamineRecordDO::getExamineStatus, 10);
            }});
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            String name = EmployeeStatusEnum.getName(employeeDO.getState());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineRecordDO.getExaminePlanName());
            examineRecordDO.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(dept.getData())) {
                examineRecordDO.setDeptName(dept.getCheckedData().getName());
            }
            if (!ObjectUtils.isEmpty(post.getData())) {
                examineRecordDO.setPostName(post.getCheckedData().getName());

            }
            examineRecordDO.setEmployeeStatus(name);
            examineRecordDO.setExamineNum(l.intValue());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
                examineRecordDO.setGradeName("");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }

            switch (examineRecordDO.getCycleType()) {
                case 1:
                    examineRecordDO.setCycleTypeStr("月度");
                    break;
                case 2:
                    examineRecordDO.setCycleTypeStr("季度");
                    switch (examineRecordDO.getCycleTimeType()) {
                        case "1":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第一季度");
                            break;
                        case "2":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第二季度");
                            break;
                        case "3":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第三季度");
                            break;
                        case "4":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第四季度");
                            break;
                    }
                    break;
                case 3:
                    examineRecordDO.setCycleTypeStr("上半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 4:
                    examineRecordDO.setCycleTypeStr("下半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 5:
                    examineRecordDO.setCycleTypeStr("全年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 6:
                    examineRecordDO.setCycleTypeStr("其他");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getCycleStartTime() + "-" + examineRecordDO.getCycleEndTime());
                    break;
            }
        });
        return examineRecordDOS;
    }

    @Override
    @DataPermission(enable = false)
    public PageResult<ExamineRecordDO> selectEmployeeExamineFileList(Integer pageNum, Integer pageSize) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        PageParam param = new PageParam();
        param.setPageSize(pageSize);
        param.setPageNo(pageNum);
        PageResult<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectPage(param, new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineStatus, 10);
            eq(ExamineRecordDO::getEmployeeId, loginId);
        }});
        examineRecordDOS.getList().forEach(examineRecordDO -> {
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
        });
        return examineRecordDOS;
    }

    @Override
    @DataPermission(enable = false)
    public List<ExamineRecordDO> selectEmployeeExamineExcelFileList() {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();

        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineStatus, 10);
            eq(ExamineRecordDO::getEmployeeId, loginId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }


            switch (examineRecordDO.getCycleType()) {
                case 1:
                    examineRecordDO.setCycleTypeStr("月度");
                    break;
                case 2:
                    examineRecordDO.setCycleTypeStr("季度");
                    switch (examineRecordDO.getCycleTimeType()) {
                        case "1":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第一季度");
                            break;
                        case "2":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第二季度");
                            break;
                        case "3":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第三季度");
                            break;
                        case "4":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第四季度");
                            break;
                    }
                    break;
                case 3:
                    examineRecordDO.setCycleTypeStr("上半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 4:
                    examineRecordDO.setCycleTypeStr("下半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 5:
                    examineRecordDO.setCycleTypeStr("全年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 6:
                    examineRecordDO.setCycleTypeStr("其他");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getCycleStartTime() + "-" + examineRecordDO.getCycleEndTime());
                    break;
            }
        });
        return examineRecordDOS;
    }


    @Override
    @DataPermission(enable = false)
    public List<ExamineRecordDO> selectExamineFileInfoList(Integer employeeId, String examinePlanName) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineStatus, 10);
            eq(ExamineRecordDO::getEmployeeId, employeeId);
            like(!ObjectUtils.isEmpty(examinePlanName), ExamineRecordDO::getExamineName, examinePlanName);
            orderByDesc(ExamineRecordDO::getId);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
        });
        return examineRecordDOS;
    }


    public List<ExamineRecordDO> selectExamineFileInfoExcelList(Integer employeeId, String examinePlanName) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineStatus, 10);
            eq(ExamineRecordDO::getEmployeeId, employeeId);
            like(!ObjectUtils.isEmpty(examinePlanName), ExamineRecordDO::getExamineName, examinePlanName);
        }});
        examineRecordDOS.forEach(examineRecordDO -> {
            ExamineDO examineDO = examineMapper.selectById(examineRecordDO.getExamineId());
            EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
            examineRecordDO.setEmployeeName(employeeDO.getName());
            examineRecordDO.setExaminePlanName(examineDO.getExaminePlanName());
            if (examineRecordDO.getExamineGradeId() == -1) {
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordDO.getExamineGradeId());
                examineRecordDO.setGradeName(examineResultConfigDO.getGradeName());
            }
            switch (examineRecordDO.getCycleType()) {
                case 1:
                    examineRecordDO.setCycleTypeStr("月度");
                    break;
                case 2:
                    examineRecordDO.setCycleTypeStr("季度");
                    switch (examineRecordDO.getCycleTimeType()) {
                        case "1":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第一季度");
                            break;
                        case "2":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第二季度");
                            break;
                        case "3":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第三季度");
                            break;
                        case "4":
                            examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear() + "年 第四季度");
                            break;
                    }
                    break;
                case 3:
                    examineRecordDO.setCycleTypeStr("上半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 4:
                    examineRecordDO.setCycleTypeStr("下半年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 5:
                    examineRecordDO.setCycleTypeStr("全年");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getExamineYear());
                    break;
                case 6:
                    examineRecordDO.setCycleTypeStr("其他");
                    examineRecordDO.setCycleTimeType(examineRecordDO.getCycleStartTime() + "-" + examineRecordDO.getCycleEndTime());
                    break;
            }
        });
        return examineRecordDOS;
    }

    @Override
    @DataPermission(enable = false)
    public ExamineRecordDO selectEmployeeExamineFileInfoList(Integer examineRecordId) {
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(examineRecordId);
        EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
        List<ExamineDimensionIndexDO> examineDimensionIndexDOS = examineDimensionIndexMapper.selectList(new LambdaQueryWrapper<ExamineDimensionIndexDO>() {{
            eq(ExamineDimensionIndexDO::getExamineId, examineRecordDO.getExamineId());
        }});
        examineDimensionIndexDOS.forEach(examineDimensionIndexDO -> {
            ExamineDimensionDO examineDimensionDO = examineDimensionMapper.selectById(examineDimensionIndexDO.getIndexDimensionId());
            examineDimensionIndexDO.setDimensionName(examineDimensionDO.getDimensionName());
            examineDimensionIndexDO.setDimensionWeight(examineDimensionDO.getDimensionWeight());
        });
        examineRecordDO.setExamineDimensionIndexDOList(examineDimensionIndexDOS);
        examineRecordDO.setEmployeeName(employeeDO.getName());
        List<ExamineRecordRaterDO> examineRecordRaterDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getRecordId, examineRecordDO.getId());
            orderByAsc(ExamineRecordRaterDO::getScorerOrder);
        }});
        List<JSONObject> commentResultList = new ArrayList<>();
        examineRecordRaterDOS.forEach(examineRecordRaterDO -> {
            System.err.println(examineRecordRaterDO.getRaterEmployeeId());
            EmployeeDO employeeResultDO = employeeMapper.selectByUserId(examineRecordRaterDO.getRaterEmployeeId().longValue());
            JSONObject commentResult = new JSONObject();
            commentResult.put("comment", examineRecordRaterDO.getRaterComment());
            commentResult.put("employeeName", employeeResultDO.getName());
            commentResult.put("fraction", examineRecordRaterDO.getRaterFraction());
            commentResult.put("weight", examineRecordRaterDO.getRaterWeight());
            if (examineRecordRaterDO.getRaterGradeId() == -1) {
                commentResult.put("gradeName", "");
            } else {
                ExamineResultConfigDO examineResultConfigDO = examineResultConfigMapper.selectById(examineRecordRaterDO.getRaterGradeId());
                commentResult.put("gradeName", examineResultConfigDO.getGradeName());
            }
            commentResultList.add(commentResult);
        });
        examineRecordDO.setCommentResults(commentResultList);

        return examineRecordDO;
    }

    @Override
    public JSONObject selectFileTitle(Integer employeeId) {
        EmployeeDO employeeDO = employeeMapper.selectByUserId(employeeId.longValue());
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
        String name = EmployeeStatusEnum.getName(employeeDO.getState());
        JSONObject result = new JSONObject();
        result.put("employeeName", employeeDO.getName());
        if (!ObjectUtils.isEmpty(dept.getData())) {
            result.put("deptName", dept.getCheckedData().getName());
        } else {
            result.put("deptName", "");

        }
        if (!ObjectUtils.isEmpty(post.getData())) {
            result.put("postName", post.getCheckedData().getName());
        } else {
            result.put("postName", "");

        }
        return result;
    }

    @Override
    public JSONObject selectjdTitle(Integer examineId) {
        List<ExamineRecordDO> examineRecordDOS = examineRecordMapper.selectList(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getExamineId, examineId);
        }});
        ExamineDO examineDO = examineMapper.selectById(examineId);
        if (ObjectUtils.isEmpty(examineDO)) {
            throw new ResultException("考核已终止");
        }
        JSONObject result = new JSONObject();
        int size = examineRecordDOS.size();
        for (int i = 0; i < examineRecordDOS.size(); i++) {
            if (i != size - 1) {
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDOS.get(i).getEmployeeId().longValue());
                if (!ObjectUtils.isEmpty(employeeDO)) {
                    result.put("employeeName", employeeDO.getName() + ",");
                }

            } else {
                EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDOS.get(i).getEmployeeId().longValue());
                if (!ObjectUtils.isEmpty(employeeDO)) {
                    result.put("employeeName", employeeDO.getName());
                }
            }
        }
        result.put("cycleType", examineDO.getExamineCycleTypeId());
        if (examineDO.getExamineCycleTypeId() == 3 || examineDO.getExamineCycleTypeId() == 4 || examineDO.getExamineCycleTypeId() == 5) {
            result.put("cycleTimeType", examineDO.getExamineYear() + "年");
        } else if (examineDO.getExamineCycleTypeId() == 6) {
            result.put("cycleTimeType", examineDO.getExamineCycleStart() + "-" + examineDO.getExamineCycleEnd());
        } else if (examineDO.getExamineCycleTypeId() == 2) {
            if (examineDO.getExamineCycleTimeType().equals("1")) {
                result.put("cycleTimeType", "第一季度");
            } else if (examineDO.getExamineCycleTimeType().equals("2")) {
                result.put("cycleTimeType", "第二季度");
            } else if (examineDO.getExamineCycleTimeType().equals("3")) {
                result.put("cycleTimeType", "第三季度");
            } else if (examineDO.getExamineCycleTimeType().equals("4")) {
                result.put("cycleTimeType", "第四季度");
            }
            result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        } else {
            result.put("cycleTimeType", examineDO.getExamineCycleTimeType());
        }
        result.put("examineNum", size);
        result.put("examineName", examineDO.getExaminePlanName());
        result.put("status", examineDO.getExamineState());
        return result;
    }

    @Override
    public List<JSONObject> selectExamineFileListInfo(Integer examineId) {
        JSONObject result = new JSONObject();
        result.put("employeeName", "张三");
        return null;
    }

    @Override
    public Integer insertIsLowFile(Integer examineRecordId, String file) {
        if (file == null) {
            throw new ResultException("上传失败");
        }
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(examineRecordId);

        if (examineRecordDO.getIsLowPerformanceFile() != null) {
            examineRecordDO.setIsLowPerformanceFile(file + "," + examineRecordDO.getIsLowPerformanceFile());
        } else {
            examineRecordDO.setIsLowPerformanceFile(file);
        }

        examineRecordDO.setIsLowPerformance(2);
        examineRecordMapper.updateById(examineRecordDO);
        EmployeeDO employeeDO = employeeMapper.selectByUserId(examineRecordDO.getEmployeeId().longValue());
        riskService.deleteRisk(employeeDO.getId(), 2000L);
        return 0;
    }


    @Override
    public List<FileRefRespDTO> selectIsLowFile(Integer examineRecordId) {
        List<Integer> list = new ArrayList<>();
        ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(examineRecordId);
        String isLowPerformanceFile = examineRecordDO.getIsLowPerformanceFile();
        if (isLowPerformanceFile == null) {
            return new ArrayList<>();
        }
        String[] split = isLowPerformanceFile.split(",");
        if (!ObjectUtils.isEmpty(split)) {
            for (int i = 0; i < split.length; i++) {
                if (ObjectUtils.isEmpty(split[i])) {
                    continue;
                }
                Integer s = Integer.parseInt(split[i]);
                list.add(s);
            }
            if (list.size() > 0) {
                List<FileRefRespDTO> list1 = fileApi.getList(list).getCheckedData();
                return list1;
            } else {
                return new ArrayList<>();
            }
        } else {
            return new ArrayList<>();
        }

    }

    @Override
    public JSONObject selectDealtWithNum() {
        Long eId = 0L;
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        Long qrNum = examineRecordMapper.selectCount(new LambdaQueryWrapper<ExamineRecordDO>() {{
            eq(ExamineRecordDO::getEmployeeId, loginId);
            eq(ExamineRecordDO::getSendStatus, 1);
            eq(ExamineRecordDO::getExamineStatus, 5);
        }});

        Long pfNUm = examineRecordRaterMapper.selectCount(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
            eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
            in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
        }});
        Long shNum = examineResultMapper.selectCount(new LambdaQueryWrapper<ExamineResultDO>() {{
            eq(ExamineResultDO::getResultEmployeeId, loginId);
            eq(ExamineResultDO::getResultStatus, 1);
        }});

        EmployeeDO employeeDO = employeeMapper.selectByUserId(loginId);
        if (!ObjectUtils.isEmpty(employeeDO)) {
            eId = employeeDO.getId();
        }
        Long finalEId = eId;
        Long kqNum = dingAttendanceConfirmationMapper.selectCount(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getEmployeeId, finalEId).eq(DingAttendanceConfirmation::getSendStatus, 0).eq(DingAttendanceConfirmation::getSendStatus, 1).or().
                    eq(DingAttendanceConfirmation::getEmployeeId, finalEId).eq(DingAttendanceConfirmation::getConfirmStatus, 0).eq(DingAttendanceConfirmation::getSendStatus, 1);
        }});

        Long salaryNum = hrmSalaryRecordEmployeeMapper.selectCount(new LambdaQueryWrapper<HrmSalaryRecordEmployee>() {{
            eq(HrmSalaryRecordEmployee::getEmployeeId, finalEId).eq(HrmSalaryRecordEmployee::getSendStatus, 1).eq(HrmSalaryRecordEmployee::getCheckStatus, 0).or()
                    .eq(HrmSalaryRecordEmployee::getEmployeeId, finalEId).eq(HrmSalaryRecordEmployee::getSendStatus, 1).eq(HrmSalaryRecordEmployee::getConfirmStatus, 0);
        }});
        HrmEContractTaskParam param = new HrmEContractTaskParam();
        param.setEmpId(eId);
        param.setTaskStatus(7);
        //待签署
        Integer toSignCount = hrmEContractTaskMapper.getTaskCountSqlApp(param);
        //待填写
        param.setTaskStatus(4);
        Integer toFillCount = hrmEContractTaskMapper.getTaskCountSqlApp(param);
        JSONObject result = new JSONObject();
        result.put("qrNum", qrNum);
        result.put("pfNUm", pfNUm);
        result.put("shNum", shNum);
        result.put("kqNum", kqNum);
        result.put("salaryNum", salaryNum);
        result.put("toSignCount", toSignCount);
        result.put("toFillCount", toFillCount);
        return result;
    }

    @Override
    public Integer progressMessage(Integer type, Integer recordId) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        if (type == 1) {

            Long num = examineRecordRaterMapper.selectCount(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginUserId);
                eq(ExamineRecordRaterDO::getRecordId, recordId);
                in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
            }});

            if (num > 0) {
                return 1;
            } else {
                return 2;
            }

        } else if (type == 2) {
            Long num = examineResultMapper.selectCount(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getResultEmployeeId, loginUserId);
                in(ExamineResultDO::getResultStatus, 1, 3);
                eq(ExamineResultDO::getRecordId, recordId);
            }});
            if (num > 0) {
                return 3;
            } else {
                return 4;
            }

        } else if (type == 3) {
            ExamineRecordDO examineRecordDO = examineRecordMapper.selectById(recordId);
            if (ObjectUtils.isEmpty(examineRecordDO)) {
                throw new ResultException("考核已终止");
            }
            if (examineRecordDO.getExamineStatus() == 5) {
                return 5;


            } else {
                return 6;
            }
        }

        return 0;
    }


    public List<Integer> selectExamineIdListByLoginId(Integer loginId, Integer status) {
        if (status == 1) {
            List<ExamineRecordRaterDO> examineRecordDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                in(ExamineRecordRaterDO::getRaterStatus, 0, 6);
            }});
            if (ObjectUtils.isEmpty(examineRecordDOS)) {
                return null;
            }
            List<Integer> collect = examineRecordDOS.stream().map(ExamineRecordRaterDO::getExamineId).collect(Collectors.toList());
            return collect;
        } else if (status == 2) {
            List<ExamineRecordRaterDO> examineRecordDOS = examineRecordRaterMapper.selectList(new LambdaQueryWrapper<ExamineRecordRaterDO>() {{
                eq(ExamineRecordRaterDO::getRaterEmployeeId, loginId);
                ge(ExamineRecordRaterDO::getRaterStatus, 0);
                ne(ExamineRecordRaterDO::getRaterStatus, 6);
            }});
            if (ObjectUtils.isEmpty(examineRecordDOS)) {
                return null;
            }
            List<Integer> collect = examineRecordDOS.stream().map(ExamineRecordRaterDO::getExamineId).collect(Collectors.toList());
            return collect;
        } else if (status == 3) {
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getResultStatus, 1);
            }});
            if (ObjectUtils.isEmpty(examineResultDOS)) {
                return null;
            }
            List<Integer> collect = examineResultDOS.stream().map(ExamineResultDO::getExamineId).collect(Collectors.toList());
            return collect;
        } else if (status == 4) {
            List<ExamineResultDO> examineResultDOS = examineResultMapper.selectList(new LambdaQueryWrapper<ExamineResultDO>() {{
                eq(ExamineResultDO::getResultStatus, 2);
            }});
            if (ObjectUtils.isEmpty(examineResultDOS)) {
                return null;
            }
            List<Integer> collect = examineResultDOS.stream().map(ExamineResultDO::getExamineId).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    public void sendMessage(String info, Integer employeeId) {

//        List<EmployeeDO> employeeDOS=new L
//        Map<String, Object> templateParams = new HashMap<>(); // 站内信内容
//        templateParams.put("info", info );
////        List<EmployeeDO> birthdayEmployee = employeeDOList.stream().filter(e -> birthdayIds.contains(e.getId())).collect(Collectors.toList());
//        Map<String, Object> birthdayMap = new HashMap<>(); // 站内信对应业务模块接口入参
//        birthdayMap.put("pageNo", 1);
//        birthdayMap.put("pageSize", 10);
//        birthdayMap.put("isNotify", true);
//        String birthdayOptions = JsonUtils.toJsonString(birthdayMap);
//        notifyList.addAll(buildNotifyList(birthdayEmployee, birthdayOptions, templateParams, NotifyDetailTypeEnum.BIRTHDAY.getCode()));


    }
}